How to click on the toolbar <dd> element? - python

I'm developing test automation and I want to click on element located in toolbar,I'm using python and selenium. Here is a code of website
<dl class="ToolbarDropdown_menu_2wY9X ToolbarDropdown_isHidden_3UaGr" tabindex="0" style=""
xpath="1"><dt><span class="ToolbarDropdown_title_1NBVn">New</span>
<span class="DropdownArrow_arrowIcon_dDzph DropdownArrow_down_3dlvo"></span>
</dt>
<dd class="ToolbarDropdown_item_nP-_M" style="">Dataset</dd>
<dd class="ToolbarDropdown_item_nP-_M">Project</dd>
</dl>
I need to click on element with text Dataset.
I locate element in this way
DATASET_BUTTON = (By.XPATH, '//dd[contains(text(),"Dataset")]')
And I want to perform action like that
self.driver.find_element(*VideosPageLocator.DATASET_BUTTON).click()
And there is no action but in terminal looks like my step has passed. Do you have any idea how to click on element in dropdown?

Maybe the action is done before DOM has loaded completely.
Try using WebDriverWait.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
web_driver_wait = WebDriverWait(self.driver, 10)
element = web_driver_wait.until(EC.element_to_be_clickable((By.XPATH,'//dd[contains(text(),"Dataset")]')))
element.click()

To invoke click() on the element with text as Dataset you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//dl[starts-with(#class, 'ToolbarDropdown')]//dd[starts-with(#class, 'ToolbarDropdown_item') and text()='Dataset']"))).click()
Note : You have to add the following imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

How to select prompts on a dropdown menu using Selenium and Python

My goal is to search for an item on the microcenter store. However, I need to select a store to choose in a dropdown menu so that I can get the proper search results. I cannot figure out how to do this. For some reason driver.find_elements_by_class_name does not work to open up the dropdown.
Here is my code:
#selenium imports
from re import search
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PATH = "C:\Program Files (x86)/chromedriver.exe"
driver = webdriver.Chrome(PATH)
what_search = "card"
driver.get("https://www.microcenter.com/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-:-MicroCenter")
#searches for what_search
print(driver.title)
search = driver.find_element_by_name("Ntt")
search.send_keys(what_search)
search.send_keys(Keys.RETURN)
#this opens up the dropdown menu
store = driver.find_element_by_id("Change-Store")
store.click()
time.sleep(1)
#these are my "attempts" to open up and click on a store location option
#storeclick = driver.find_element_by_class_name("btn btn-secondary dropdown-toggle selectorHref")
#storeclick = driver.find_element_by_class_name("dropdown-item")
storeclick = driver.find_elements_by_class_name("dropdown-itemLI store_055")
print(storeclick)
storeclick.click()
print("I TRIED")
The main issue is:
storeclick = driver.find_element_by_class_name("btn btn-secondary dropdown-toggle selectorHref")
does not work to find the dropdown menu button
<li class="dropdown-itemLI store_055"><a class="dropdown-item" href="/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-%3a-MicroCenter&storeid=055">MI - Madison Heights</a></li>
Thus I cannot click on it.
I want to click on the dropdown menu, then select a store option:
Using only class name can be tricky to find if the webpage is too big. Try using xpath instead so you can use more filters. Here is an example that can find the button:
Element:
<li class="dropdown-itemLI store_055"><a class="dropdown-item" href="/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-%3a-MicroCenter&storeid=055">MI - Madison Heights</a></li>
Xpath: //li[#class="dropdown-itemLI store_055"]//a[#class="dropdown-item"]
storeclick = drive.find_elements_by_xpath('//li[#class="dropdown-itemLI store_055"]//a[#class="dropdown-item"]')
To click on the element with text as MI - Madison Heights you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Code Block:
driver.get("https://www.microcenter.com/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-:-MicroCenter")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-light.accept"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "MI - Madison Heights"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:

NoSuchElementException: could not click the div element

<td role="presentation" valign="top" class=" x-trigger-cell x-unselectable" style="width:28px;" id="ext-gen1147">
<div class="x-trigger-index-0 x-form-trigger x-form-arrow-trigger x-form-trigger-first rp-icon-expanded" role="presentation" id="ext-gen1146">
</div></td>
im trying to click this div elementbut throwing exception.My code is:
driver.find_element_by_xpath("//div[#id='ext-gen1146']").click()
driver.find_element_by_xpath("//*[#id='ext-gen1147']").click()
driver.find_element_by_xpath("//div[#class='x-trigger-index-0 x-form-trigger x-form-arrow-trigger x-form-trigger-first rp-icon-expanded' and #id='ext-gen1146']")
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element:
{"method":"xpath","selector":"//*[#id='ext-gen1147']"} (Session
info: chrome=80.0.3987.163)
Try below solution and check your element is not dynamic and its not inside the iframe, if its in iframe then you have to switch to it and then click on your eelement.
wait.until(EC.element_to_be_clickable((By.ID, "ext-gen1147"))).click()
or
wait.until(EC.element_to_be_clickable((By.XPATH, "x-trigger-index-0 x-form-trigger x-form-arrow-trigger x-form-trigger-first rp-icon-expanded"))).click()
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
To switch to iframe you can use below code, Make sure you have only one iframe or else you can use ID/Name of iframe to identify correct element before switching:
iframe = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.TAG_NAME, 'iframe')))
driver.switch_to.frame(iframe)
I think maybe you need to wait for visibility first, maybe this helps you https://stackoverflow.com/a/19537085/2069610... for example:
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<"ext-gen1146>));
To click on dynamic element Induce WebDriverWait() and element_to_be_clickable() and following css selector.
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.rp-icon-expanded[id^='ext-gen'][role='presentation']"))).click()
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
you can try Actions class to click on the expected element -
Actions actions = new Actions(driver);
actions.click(element).build().perform();
if this also not works try using JavascriptExecutor
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor..executeScript("arguments[0].click();", element);

Difficult button to press in selenium

how to press the "f_agreements_all" button ?
<label for="f_agreements_all">
<input type="checkbox" id="f_agreements_all">
<span></span>
<span class="permText">I accept all</span>
</label>
Unfortunately, finding "f_agreements_all" button and clicking it doesnt work. Span covers the whole button17x17px, when f_agreements_all is 16x16 under it. Do you know possible way to click it?
Using vanilla JS one way to do it would be:
const checkbox = document.getElementById('f_agreements_all');
checkbox.click();
Quickly looking at the Python Selenium 2 WebDriver API docs, one can try:
driver.find_element_by_id("f_agreements_all").click()
Can you please try below xpath :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
button=WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, "//li[#class='formCheckbox agreements all-agreements']//span[1]")))
button.click()
To click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='f_agreements_all']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#for='f_agreements_all']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to access the 2nd checkbox that have the same class name and tag?

I am trying to access a 2nd or 3rd Checkbox Element that have the same class name and tag..Each of the checkbox is enclosed in a separate div and the div also has the same class name.
I previously searched on stackoverflow and Google and tried some of the following but none of them is working
driver.find_element_by_xpath("(//div)[#class='classname value'])[2]")
driver.find_element_by_xpath("(//div[#class='classname value']) [position()=2]")
Also
driver.find_element_by_xpath("(//span)[#class='classname value'])[2]")
driver.find_element_by_xpath("(//span[#class='classname value']) [position()=2]")
This is somehow the elements look like by inspection
<div class='a checkbox'>
<label><input type='checkbox'><span class='b'>Paid</span></label>
</div>
<div class='a checkbox'>
<label><input type='checkbox'><span class='b'>Free</span></label>
</div>
I basically want to access the checkbox with Free Text..My Overall Code is this
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver=webdriver.Firefox()
driver.get("http://udemy.com/courses/search/?src=ukw&q=python&p=1")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='All Filters']"))).click()
time.sleep(10)
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[#class='modal-body']//label[.//span[#data-purpose='filter-option-title' and text()='Free']]/input[#type='checkbox']"))).click()
You may use contain If the DOM only contain only one Free text as shown below example.
driver.find_elements_by_xpath("//*[contains(text(), 'Free')]")
If the DOM contain multiple Free text, then you need to pass it to List and based on the text position you need to iterate the List.
Amend your code to be looking like this:
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver=webdriver.Firefox()
driver.get("http://udemy.com/courses/search/?src=ukw&q=python&p=1")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[.//span[text()='All Filters']]"))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//label[#title and .//span/span[text()='Free']]"))).click()
As it is a <input> element presumably moving ahead you need to invoke click() on the element with respect to the text Free, you you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH 1:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='a checkbox']//label//span[#class='b' and text()='Free']//preceding::input[1]"))).click()
Using XPATH 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='b' and text()='Free']//preceding::input[1]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try the following xpath to click on Free checkbox on udemy account.
//div[#class='modal-body']//label[.//span[#data-purpose='filter-option-title' and text()='Free']]/input[#type='checkbox']
Induce WebDriverWait and element_to_be_clickable() And Following XPATH locator.
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[#class='modal-body']//label[.//span[#data-purpose='filter-option-title' and text()='Free']]/input[#type='checkbox']"))).click()
Here is the code.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver=webdriver.Chrome("path of the chrome driver")
driver.get("http://udemy.com/courses/search/?src=ukw&q=python&p=1")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='All Filters']"))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[#class='modal-body']//label[.//span[#data-purpose='filter-option-title' and text()='Free']]/input[#type='checkbox']"))).click()

I can not find a way to click on a button using Python and Selenium

I have a problem, I happen to have a button with these labels, I'm trying to click through Selenium but I can not find a way to click it. I tried to give it taking as reference the XPath, link text, and CSS selector but I do not achieve my goal. This is the code for the button:
<a class="btn btn-flat pull-right" data-action="export_report"> <i class = "icon-export"> </ i> Export </a>
East of the XPath:
// * [# id = "reports"] / div [1] / div [2] / a
this selector:
#reports> div.span12> div.headline-action-block.pull-right> a
This is the button and my code in Python :(
Button:
My code:
I face this error:
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element
'value': value})['value']
You can try this:
openrate = driver.find_element_by_css_selector("a.btn")
openrate.click()
Assuming this is the only button, or the first button on the page. Otherwise ("a.btn.btn-flat.pull-right")
As per the HTML you have shared it seems you have invoked click() in the previous step so in this step to click() on the button with text as Export you have to induce WebDriverwait for the element to be clickable and you can use either of the following Locator Strategies :
PARTIAL_LINK_TEXT :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Export"))).click()
CSS_SELECTOR :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-flat.pull-right[data-action='export_report']>i.icon-export"))).click()
XPATH :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-flat pull-right' and #data-action='export_report']/i[#class='icon-export']"))).click()

Categories

Resources