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 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)
谷歌浏览器元素转储:
<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()
解决方案
在与元素交互之前,您必须切换到正确的 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()
推荐阅读
- php - 单击链接时如何添加查询参数,然后在 Hook 回调中检索它
- visual-studio - 如何在 Vsix 项目中访问 IWorkspace?
- debugging - 问号而不是调试器内存视图中的实际内存内容,这是什么意思?
- r - 如何向该图添加图例(使用 ggplot())?
- javascript - Dictionary .has("key") 不起作用,必须使用 dictionary["key"] != "undefined"
- node.js - 尽管我必须这样做,但使用相同的初始向量进行加密是否被认为是一种不好的做法
- javascript - Firebase 实时数据库查询不适用于更大的数字
- spring - 为什么 Spring 允许实例化具有 Private 构造函数的 bean?
- airflow - BigQueryInsertJobOperator - 缺少必需的参数,但是哪个?
- php - CORS 策略已阻止从源“http://localhost:4200”访问“http://localhost/api/car/create.php”处的 XMLHttpRequest