VincFort
VincFort

Reputation: 1180

Python + Selenium can't find element by XPath

I am trying to click on the "chercher" button on the left of the page (middle).

url = "https://www.fpjq.org/repertoires/repertoire-des-medias/"

driver = webdriver.Firefox()

driver.get(url)
time.sleep(2)
driver.find_element_by_xpath('//*[@id="recherche"]/input[3]').click()

However, it can't find the element. I copy pasted the XPath so I am not sure why it's not working.

Thanks.

Upvotes: 1

Views: 484

Answers (1)

Andersson
Andersson

Reputation: 52665

That's because required button located inside an iframe and to be able to click it you need to switch to that iframe:

url = "https://www.fpjq.org/repertoires/repertoire-des-medias/"

driver = webdriver.Firefox()

driver.get(url)
time.sleep(2)
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_xpath('//*[@id="recherche"]/input[3]').click()

Also note that using time.sleep() is not a good practice. You can try to implement Explicitwait instead

Upvotes: 2

Related Questions