Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python+;硒罐';无法通过XPath查找元素_Python_Selenium - Fatal编程技术网

Python+;硒罐';无法通过XPath查找元素

Python+;硒罐';无法通过XPath查找元素,python,selenium,Python,Selenium,我试图点击页面左侧(中间)的“chercher”按钮 但是,它找不到元素。我复制粘贴了XPath,所以我不确定它为什么不工作 谢谢。这是因为必需的按钮位于iframe内,要单击它,您需要切换到该iframe: url = "https://www.fpjq.org/repertoires/repertoire-des-medias/" driver = webdriver.Firefox() driver.get(url) time.sleep(2) driver.switch_to.fra

我试图点击页面左侧(中间)的“chercher”按钮

但是,它找不到元素。我复制粘贴了XPath,所以我不确定它为什么不工作


谢谢。

这是因为必需的按钮位于iframe内,要单击它,您需要切换到该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()

还要注意,使用
time.sleep()
不是一个好的做法。您可以尝试实现,它不再抛出错误,但是,它仍然不会单击按钮。尝试
driver。通过xpath('/*[@id=“recherche”]/input[3]')查找元素。submit()
instead谢了,这很有效。找到了另一个解决方案,单击上面的一个选项并按enter键,但这是一个更优雅的解决方案。
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()