I'm new to python and I'm trying to write a web scraping script. I'm trying to double click on this element (it's neither a button nor link - just a td element) and am having trouble with even finding it in the first place.
Below is the code
<td align="left" valign="middle" class=" "
title="Business Profile (Imported)">Business Profile (Imported)</td>
When I select it, the class changes. I suspect this is where the problem is at
<td align="left" valign="middle" class=" cellselected "
title="Business Profile (Imported)">Business Profile (Imported)</td>
I used css selector & xpath. None works. I tried both:
driver.find_element_by_xpath('//td[#title="Business Profile (Imported)"]').click()
driver.find_element_by_css_selector("td[title='Business Profile (Imported)']")
This is the error I get:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//td[#title="Business Profile (Imported)"]"}
Any help would be tremendously appreciated. Thanks!!
To locate the desired <td> element you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
XPATH 1:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[contains(#title, 'Imported') and starts-with(., 'Business Profile')]")))
XPATH 2:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[starts-with(#title, 'Business Profile') and contains(., 'Imported')]")))
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
To handle dynamic element induce WebDriverWait and element_to_be_clickable and use the following xpath
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//td[#title='Business Profile (Imported)' and text()='Business Profile (Imported)']"))).click()
OR
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//td[#title='Business Profile (Imported)'][contains(.,'Business Profile (Imported)')]"))).click()
You need to imports following to execute above code.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Related
H, I am trying to click on the below checkbox.
<thead class="ant-table-thead" xpath="1">
<tr>
<th class="ant-table-selection-column"><span class="ant-table-header-column"><div><span class="ant-table-column-title"><div class="ant-table-selection"><label class="ant-checkbox-wrapper"><span class="ant-checkbox"><input type="checkbox" class="ant-checkbox-input" value=""><span class="ant-checkbox-inner"></span></span>
</label>
</div>
</span><span class="ant-table-column-sorter"></span></div>
</span>
</th>
</tr>
</thead>
It gets clicked instantly only as follows:
Checkbox = "//thead/tr[1]/th[1]/span[1]/div[1]/span[1]/div[1]/label[1]/span[1]/input[1]"
driver.find_element_by_xpath(Checkbox).click()
However, when I try to add explicit wait, the checkbox doesn't get clicked
if WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, Checkbox))): driver.find_element_by_xpath(
Checkbox).click()
I have also tried EC.visibility_of_element_located but also doesn't work
Thanks for the help in advance
looking at your code and at the documentation i think i figured out, i cannot reproduce to test but i will try to explain why isn't working:
From the doc:
from selenium.common.exceptions import TimeoutException
try:
element = WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH, Checkbox))
driver.find_element_by_xpath(Checkbox).click()
except TimeoutException as ex :
*put here the "else" code, it will be execute if the wait ends with no Checkbox found so the WebDriver will Throw a TimeoutException*
To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "thead.ant-table-thead input.ant-checkbox-input"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//thead[#class='ant-table-thead']//input[#class='ant-checkbox-input']"))).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
I am trying to click on the following button using Selenium in Python. However, any way I have tried to approach clicking on that button has failed and I was hoping someone could help. For all intents and purposes. I am a novice in both Python/Selenium so any help would be greatly appreciated!
<tr>
<td colspan=3></td>
<td align=center valign='center'> <a href="javascript:myFunction('/mailbox/jsp/MBIList.jsp')"
onMouseOver="chgButton('Go','GoOver'); window.status=''; return true;"
onMouseOut="chgButton('Go','GoOff'); window.status=''; return true;"
onMouseDown="chgButton('Go','GoDown'); return true;"><img border=0
src="/mailbox/images/go_off.gif" vspace=7 name='Go' align='top'></a>
</td>
</tr>
U can click on any element using any of the element attributes. For example in this case I would use attribute #name='Go' in your Xpath, where you are trying to select element.
The desired element is a dynamic element, so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='/mailbox/jsp/MBIList'] > img[name='Go'][src^='/mailbox/images/go_off']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href, '/mailbox/jsp/MBIList')]/img[#name='Go' and starts-with(#src, '/mailbox/images/go_off')]"))).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
Considering the HTML:
I want to select the paragraphs to the left with Selenium. Tried my hand at class_name and id but got NoSuchElementException. Why am I getting this error? I mean the elements are clearly there then why isn't Selenium recognizing those?
Methods I tried:
element = driver.find_element_by_xpath("//div[#id = 'mar-2019']//div[#class='report_data']").text
element = driver.find_element_by_id("mar-2019").text
element = driver.find_element_by_class_name("report_data").text
Where am I going wrong?
To handle dynamic element induce WebDriverWait() and wait for visibility_of_element_located()
element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,"mar-2019"))).text
OR
element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"div#mar-2019"))).text
You need to import following libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
There are multiple child <p> elements within multiple parent/ancestor <div> elements. To extract the contents of the <p> elements within the parent <div id="mar-2019"> element you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and get_attribute("innerHTML"):
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".data-row")))])
Using XPATH and text attribute:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#id='mar-2019']//div[#class='report_data']//p")))])
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
Reference
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate 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);
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