Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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_Xpath - Fatal编程技术网

Python 硒罐';无法找到元素,这是一个使用xpath的搜索按钮

Python 硒罐';无法找到元素,这是一个使用xpath的搜索按钮,python,selenium,xpath,Python,Selenium,Xpath,我找不到一个解决方案,无法通过使用selenium xpath找到该元素。其他网站上的其他按钮总是工作正常。通常我会做的是,打开一个页面并检查元素,然后右键单击它并复制xpath。完成了 但该网站www.gsc.com.my(马来西亚电影预订网站)。似乎找不到按钮。它是否受另一个安全层保护 让我们看看下面的代码 from selenium import webdriver chromedriver_path = './chromedriver.exe' driver = webdriver

我找不到一个解决方案,无法通过使用selenium xpath找到该元素。其他网站上的其他按钮总是工作正常。通常我会做的是,打开一个页面并检查元素,然后右键单击它并复制xpath。完成了

但该网站www.gsc.com.my(马来西亚电影预订网站)。似乎找不到按钮。它是否受另一个安全层保护

让我们看看下面的代码

from selenium import webdriver

chromedriver_path = './chromedriver.exe'

driver = webdriver.Chrome(chromedriver_path)  
driver.get('https://www.gsc.com.my')


driver.find_element_by_xpath("""//*[@id="btnNext"]""").click()
错误消息:

no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="btnNext"]"}

按钮位于iframe内,因此在单击按钮之前需要切换到该帧:

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

driver.switch_to.frame('getQuickFilter_ctrl_ifrBox')

wait(driver, 10).until(EC.element_to_be_clickable((By.ID, "btnNext"))).click()

由于有两个id为btnNext的元素,因此必须指定使用索引的元素,第一个为1,第二个为2


驱动程序。通过xpath(““”/*[@id=“btnNext”][1]”)查找元素。单击()

您可以尝试使用此css选择器:

div.container input#btnNext  
请注意,您需要首先切换到iframe,因为check按钮位于iframe

要切换到iframe,请执行以下操作:

driver.switch_to.frame(driver.find_element_by_id("getQuickFilter_ctrl_ifrBox"))  
单击时,请选中按钮

wait = WebDriverWait(driver, 10)
check_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.container input#btnNext")))  
check_button.click()

等待在Selenium中不再是必要的,被认为是不好的做法。@Bouke,从什么时候开始?你能分享一个相同的链接吗?@Bouke:你能分享任何官方链接吗?@Bouke为什么?你的意思是说什么时候设置了隐式等待?@Bouke如果你的元素在2秒后变得可见怎么办?隐式等待是唯一存在的元素,它不用于可见性或启用,因此在这种情况下需要使用显式等待。如果您从未遇到过问题,那么您仍然没有在动态环境中工作过,比如一个元素单击会使另一个元素出现。嗯,我错了,元素id在文档中是唯一的,另一个元素驻留在嵌入的iframe中。如果未找到任何元素,我不确定使用相同的方法搜索第二个元素是否有意义