找不到复选框

2024-03-29 12:43:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让我的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')">

我很困惑为什么这不起作用。我甚至在行动之间有5秒的延迟

完整代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://192.168.1.235/login.cgi")
assert "NETGEAR ProSAFE Plus Switch" in driver.title
#Locate Password
passwordInput = driver.find_element_by_id("password")
#Input switch password
passwordInput.clear()
passwordInput.send_keys("password")
passwordInput.send_keys(Keys.RETURN)
#Wait for mainpage to load
time.sleep(5)
#Switch to Port Status
portStatusButton = driver.find_elements_by_xpath('//*[@id="blueLinkBold11"]/div[2]/a')
print(portStatusButton)
portStatusButton[0].click()
time.sleep(5)
#Check select all checkbox
checkButton = driver.find_element_by_name("checkALL")
checkButton.click()
#Select option from Menu (Disable)
speedDropdown = Select(driver.find_element_by_name("SPEED"))
speedDropdown.select_by_value(2)
#Click Apply Button
applyButton = driver.find_element_by_name("btn_Apply")
applyButton.click()
driver.close()

Tags: tonamefromimportidbydriverselenium
2条回答

要单击元素,必须为element_to_be_clickable()诱导WebDriverWait,并且可以使用以下Locator Strategies之一:

  • 使用CSS_SELECTOR

    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()

相关问题 更多 >