我开始学习python硒,有一个小问题
>>> from selenium import webdriver
>>> dr = webdriver.Chrome('C:/Users/Dima/Desktop/chromedriver.exe')
>>> dr.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
>>> pole = dr.find_element_by_id('f213cb817d764e4')
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
pole = dr.find_element_by_id('f213cb817d764e4')
File "C:\Users\Dima\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 360, in
find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\Dima\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Dima\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Dima\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\selenium\webdriver\remote\errorhandler.py", line 242, in
check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element:
Unable to locate element: {"method":"id","selector":"f213cb817d764e4"}
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.45.615291
(ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17763
x86_64)
我能做什么? 我从文档中做所有事情,但是 它不起作用。
答案 0 :(得分:1)
@id
是动态生成的,因此每次运行脚本时都会有所不同。如果要选择“用户名”输入字段,请尝试按@name
进行选择:
dr.find_element_by_name('username')
答案 1 :(得分:0)
Instagram 上的用户名字段是一个 React Native 元素,因此您必须引入 WebDriverWait 然后调用send_keys()
方法如下:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("ITishnik_ArtStudio")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC