python - python中的硒:TimeoutException
问题描述
我正在尝试使用 selenium 登录 Twitter,但它一直给我错误。任何帮助,将不胜感激。
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
browser = webdriver.Chrome()
username = "elonmusk"
url = "https://twitter.com/" + username + "/followers?lang=en"
browser.get(url)
# print(url)
# browser.implicitly_wait(10) #seconds
# elem = WebDriverWait(browser,4).until(EC.visibility_of_element_located((By.XPATH("//input[@name='session[username_or_email]']")))).send_keys("Michael")
# browser.find_element_by_xpath("//input[@name='session[username_or_email]']").send_keys("Michael")
wait = WebDriverWait(browser, 2)
wait.until(EC.visibility_of_element_located((By.XPATH,"//input[@name='session[username_or_email]']"))).send_keys("person")
解决方案
正如我上面所说,提到的 XPATH 是不正确的。如果您将代码的最后一行更改为下面的代码,它将起作用。
wait.until(EC.visibility_of_element_located((By.XPATH,'//*[@id="page-container"]/div/div[1]/form/fieldset/div[1]/input'))).send_keys("person")
有关如何在 Chrome 中获取正确 XPATH 或在 Firefox 中使用 FireBug/FirePath 的更多信息,请参阅下面的问题。
有没有办法在谷歌浏览器中获取 xpath?
推荐阅读
- .htaccess - 有没有办法让 htaccess 在匹配的 URL 上返回空白页?
- python - 包含上的 Google Cloud Datastore Python3 AttributeError
- javascript - 将 sql 查询返回到 txt 文件 - Node JS
- jquery - 使用自定义模板作为评级的 Kendo ui 下拉列表 jquery 无法正确显示所选值 - Telerik kendo ui jquery
- javascript - 如何删除表单的ajax部分
- html - 内容离开屏幕(HTML CSS)
- javascript - 我找不到在 HTML 和 Javascript 中声明的变量
- mongodb - 为嵌套案例类自动创建 mongo 编解码器
- python - 如何连接具有不同 N 行的 Pandas 数据帧?
- amazon-web-services - AWS CloudWatch Logs 过滤模式问题