马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 DrWhiter 于 2021-8-8 15:01 编辑
我试着运行下方代码:from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import time # 导入模块
username = "QQ number"
password = "QQ password" # 输入QQ账号和密码(给登录用)
browser = webdriver.Chrome(executable_path="chromedriver.exe")
browser.get("https://passport.bilibili.com/login")
time.sleep(2) # 打开登录页
btn_qq = browser.find_element_by_class_name('qq') # 寻找“用QQ账号登录”按钮(下同)
actions = ActionChains(browser)
actions.click(btn_qq).perform() # 点击按钮
element = WebDriverWait(driver=browser, timeout=10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="switcher_plogin"]'))) # 检查“用账号登录”按钮是否存在(下同)
btn_up = browser.find_element_by_xpath('//*[@id="switcher_plogin"]') # 定位元素位置(下同)
time.sleep(1)
actions.click(btn_up).perform() # 点击按钮(下同)
element1 = WebDriverWait(driver=browser, timeout=10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="u"]')))
username_input = browser.find_element_by_xpath('//*[@id="u"]')
element2 = WebDriverWait(driver=browser, timeout=10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="p"]')))
password_input = browser.find_element_by_xpath('//*[@id="p"]')
username_input.send_keys(username)
time.sleep(2)
username_input.send_keys(password)
element3 = WebDriverWait(driver=browser, timeout=10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="login_button"]')))
button = browser.find_element_by_xpath('//*[@id="login_button"]')
time.sleep(1)
actions.click(button).perform()
报错如下:Traceback (most recent call last):
File "E:\文档\Python\爬虫\新建文本文档2.py", line 15, in <module>
element = WebDriverWait(driver=browser, timeout=10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="switcher_plogin"]')))
File "D:\Python\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
这该如何解决?(在开发者工具里能用Xpath找到元素) |