首页 > 解决方案 > 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")

标签: pythonpython-3.xselenium

解决方案


正如我上面所说,提到的 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?


推荐阅读