python - 尝试使用 selenium 自动注册。遇到问题
问题描述
目前正在尝试使用 Selenium 在“mail.com”上自动注册。到目前为止,我已经设法让程序转到 URL。我遇到的问题是,即使我复制了“注册”的完整 XPATH,我也会得到:
" selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[114]/td[2]"}
"
错误
这是我目前正在使用的代码:
import selenium
import time
from selenium.webdriver.common.by import By
driver = selenium.webdriver.Chrome(executable_path='pathtochromedriver')
driver.get('https://www.mail.com/')
driver.maximize_window()
# Delay added to allow elements to load on webpage
time.sleep(30)
# Find the signup element
sign_up = driver.find_element_by_xpath('/html/body/table/tbody/tr[114]/td[2]')
解决方案
尝试使用 ActionsChains 滚动以确保元素在视图中。
from selenium.webdriver.common.action_chains import ActionChains
some_page_item = driver.find_element_by_class_name('some_class')
ActionsChains(driver).move_to_element(some_page_item).click(some_page_item).perform()
还有另一个提示......而不是简单地使用 time.sleep() 来等待元素出现,而是使用 WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait_for_item = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME ,"some_class_name")))
30 是等待项目出现的秒数;但是,如果它出现在 30 秒之前,那么它将立即继续执行。如果 30 秒过去且该项目未出现,则会发生超时错误。
推荐阅读
- php - 通过 Redis 的 Laravel 队列:local.ERROR:连接到 127.0.0.1:6379 时出现读取错误
- r - R:将 summary()$coef 转换为数值向量
- azure - 无法从基于 Java 的 Rest API 访问 SharePoint 图形 API
- node.js - events.js:174 错误:运行 jest 时在 ChildProcess.target._send 处写入 EPIPE
- dask - 重启 Dask Kubernetes 集群的正确方法是什么?
- reactjs - PWA - 对根路径和子路径做出反应
- java - 在本地使用嵌入式 redis 测试用例运行时面临生菜连接被拒绝问题运行良好,但在詹金斯它失败了
- amazon-web-services - 遍历字典列表以创建具有不同值的 terraform 资源
- javascript - 使用正则表达式匹配表达式
- docker - Docker:以分离模式、交互模式运行从脚本创建的映像