2

I'm trying to automate a process using selenium and have been able to open a webpage and click on links, however I stumbled upon a table in which a link needs to be clicked but I'm unable to select that link and getting an error. need help to select that particular element

right now this is what I've done

elem2=browser.find_elements_by_xpath('/html/body/div[3]/table/tbody/tr[1]/td[2]/div[2]/table/tbody/tr[7]/td[3]/a::text')
elem2.click()

you can see in the picture that I'm trying to access the findhtml.org link.

the error that I get is

InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression /html/body/div[3]/table/tbody/tr[1]/td[2]/div[2]/table/tbody/tr[7]/td[3]/a::text because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '/html/body/div[3]/table/tbody/tr[1]/td[2]/div[2]/table/tbody/tr[7]/td[3]/a::text' is not a valid XPath expression.
  (Session info: chrome=81.0.4044.113)

enter image description here

3 Answers 3

2

First you have to switch to the iframe

Example:

frame = browser.find_elements_by_xpath('//iframe[contains(@src, \'hbx.media.net\')]')
browser.switch_to.frame(frame)

Now you can click

link = browser.find_elements_by_xpath('//a[contains(@href, \'http://www.findhtml.org\')]')
link.click()
Sign up to request clarification or add additional context in comments.

6 Comments

getting an error SyntaxError: invalid syntax under the 'x' of hbx.net
escape the url with \ or add double quotes "
now a new error NameError: name 'driver' is not defined
should it be browser instead of driver for me
I tried doing browser too instead of driver and getting this error InvalidArgumentException: Message: invalid argument: invalid 'id'
|
1

To click on the specific link try below code.

Induce WebDriverWait() and presence_of_element_located() and following xpath.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://publicrecords.netronline.com/state/IL/county/dupage")
element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//a[@href='http://www.dupageco.org/PropertyInfo/PropertyLookUp.aspx' and contains(.,'Go to Data')]")))
element.location_once_scrolled_into_view
element.click()

Please note the element is not inside any iframe

4 Comments

but can you explain to me what did these new imports did and how they helped to click on the link?
@reduxHelpPlz : its wait for the element to be available on the page and click.It get rid of synchronization issue which sometime cause error to find element.
okay. so before I was using find_element_by_link_text and then adding click() on that element which would sometimes shows interrupt or other error. so this method is more efficient and I should replace the earlier clicking event with this new clicking code?
Two things my code will do.1) This will solve the synchronization issue. 2) since item you are searching it bottom of the table so you need to scroll into element element.location_once_scrolled_into_view and then click. Hope you understood the entire code now.
1
browser.get('https://publicrecords.netronline.com/state/IL/county/dupage')

wait = WebDriverWait(browser, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//td[contains(text(),'DuPage Supervisor of Assessments')]//following-sibling::td[2]//a"))).click()

output:

enter image description here

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

6 Comments

getting error as NameError: name 'driver' is not defined
check updated solution , use browser instead of driver
TimeoutException: Message: this is the new error.. i waited for 1 minute, didn't click anywhere and got the timeout error
can you share your url ? Also where you got this error while switching or while clicking on your td element
@DipakBachhav : There is no iframe to switch.why are you providing wrong info?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.