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 找不到复选框_Python_Selenium - Fatal编程技术网

Python 找不到复选框

Python 找不到复选框,python,selenium,Python,Selenium,我试图让我的selenium脚本在网站上选中“selectALL”复选框。问题是python程序找不到它 我试过了 使用名称 使用xpath 结果如下 姓名: checkButton = driver.find_element_by_name("checkALL") checkButton.click() selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate

我试图让我的selenium脚本在网站上选中“selectALL”复选框。问题是python程序找不到它

我试过了

  • 使用名称
  • 使用xpath
结果如下

姓名:

checkButton = driver.find_element_by_name("checkALL")
checkButton.click()

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="checkALL"]"}
  (Session info: chrome=80.0.3987.132)
使用xpath:

checkButton = driver.find_element_by_xpath("//table[@id='tbl2']/tbody/tr/td/input")
checkButton.click()

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//table[@id='tbl2']/tbody/tr/td/input"}
  (Session info: chrome=80.0.3987.132)
Google Chrome元素转储:

<input type="checkbox" name="checkALL" rownumber="" value="notchecked" onclick="checkAllCheckedRows('portID')">

要单击元素,您必须引导WebDriverWait使
元素成为可单击的()
,并且您可以使用以下任一选项:

  • 使用
    CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='checkALL'][value='notchecked'][onclick^='checkAllCheckedRows']"))).click()
    
  • 使用
    XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='checkALL' and @value='notchecked'][starts-with(@onclick, 'checkAllCheckedRows')]"))).click()
    
  • 注意:您必须添加以下导入:

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

在与元素交互之前,必须切换到右侧iframe

driver.switch_to.frame(driver.find_element_by_css_selector(css_selector))
# then click on the element.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='checkALL']"))).click()

确保等待页面加载并显示元素。并检查元素是否存在于iframe中。@supputuri它肯定已经加载并显示了。但它仍然不起作用。您是否能够在chrome开发工具中找到带有
[name='checkALL']
的元素?@supputuri是的,我现在可以检查
[name='checkALL']/祖先::html
。如果它指向浏览器html,则您没有任何iframe包装元素,但如果您将
document\#
视为html元素的父元素,则获取iframe并切换到它。如果您觉得问题已解决,我会收到一个raise TimeoutException(消息、屏幕、堆栈跟踪)错误,请接受答案,并为未来的愿景投票。
driver.switch_to.frame(driver.find_element_by_css_selector(css_selector))
# then click on the element.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='checkALL']"))).click()