关于selenium元素查找的问题
本帖最后由 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找到元素) 你为什么要设置个超时呢,这也不是元素不元素的问题啊。 我看了一下,那个登录页面在一个新的iframe中,这种情况下使用selenium需要先切换到新的iframe中才能继续定位,否则定位不到。
你可以直接输入到irame对应的网页(开发者工具中可以看到)。更为标准的做法是switch_to_iframe。
你可以百度一下selenium如何定位到iframe中的元素,这应该属于一个基本操作,很多网页的登录界面都是在新的iframe中的 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="D:/PYTHON/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() # 点击按钮
time.sleep(2)
iframe = browser.find_element_by_xpath('//*[@id="ptlogin_iframe"]')
browser.switch_to_frame(iframe)
# # element = WebDriverWait(driver=browser, timeout=10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="switcher_plogin"]'))) # 检查“用账号登录”按钮是否存在(下同)
# time.sleep(3)
browser.find_element_by_xpath('//*[@id="switcher_plogin"]').click() # 定位元素位置(下同)
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)
password_input.send_keys(password)
# element3 = WebDriverWait(driver=browser, timeout=10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="login_button"]')))
browser.find_element_by_xpath('//*[@id="login_button"]').click()
# time.sleep(1)
# actions.click(button).perform() King丨小义 发表于 2021-8-10 09:11
这个是我在你的代码基础上修改的,WebDriverWait我直接改成time.sleep了,还有action.click(button).perform我没有用过,也没有运行成功,我直接改成click()了 还有推荐给你一个谷歌插件,Xpath Helper,它可以根据你的Xpath模拟爬虫程序查找一下元素,可以看出来你的xpath放在程序中是否能真正定位到元素
页:
[1]