将密钥发送到 twitter xpath 会出现错误
Twitter 今天更新了用户界面,这会是问题的原因吗?试图找到 iframe 看看是否需要切换,但找不到任何
Traceback (most recent call last):
File "c:\Users\user\Desktop\pyhton\test.py", line 20, in <module>
username = Driver.find_element(By.XPATH,'//*[@id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[4]/label/div/div[2]')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 741, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
self.error_handler.check_response(response)
File "C:\Users\user\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[4]/label/div/div[2]"}
(Session info: chrome=124.0.6367.158); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
GetHandleVerifier [0x00007FF7A31F1522+60802]
(No symbol) [0x00007FF7A316AC22]
(No symbol) [0x00007FF7A3027CE4]
(No symbol) [0x00007FF7A3076D4D]
(No symbol) [0x00007FF7A3076E1C]
(No symbol) [0x00007FF7A30BCE37]
(No symbol) [0x00007FF7A309ABBF]
(No symbol) [0x00007FF7A30BA224]
(No symbol) [0x00007FF7A309A923]
(No symbol) [0x00007FF7A3068FEC]
(No symbol) [0x00007FF7A3069C21]
GetHandleVerifier [0x00007FF7A34F41BD+3217949]
GetHandleVerifier [0x00007FF7A3536157+3488183]
GetHandleVerifier [0x00007FF7A352F0DF+3459391]
GetHandleVerifier [0x00007FF7A32AB8E6+823622]
(No symbol) [0x00007FF7A3175FBF]
(No symbol) [0x00007FF7A3170EE4]
(No symbol) [0x00007FF7A3171072]
(No symbol) [0x00007FF7A31618C4]
BaseThreadInitThunk [0x00007FF8056C7344+20]
RtlUserThreadStart [0x00007FF8063626B1+33]
我正在运行的代码
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
Driver = webdriver.Chrome()
Driver.get('https://x.com/i/flow/login')
time.sleep(3)
username = Driver.find_element(By.XPATH,'//*[@id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[4]/label/div/div[2]')
username.send_keys('test')
time.sleep(2)
期望用户名区域为“test”
//*[@id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[4]/label/div/div[2]
以上 XPath 表达式不正确。它不定位任何元素。因此
no such element
例外。
登录页面只有一个
<input>
元素。所以你可以使用 TAG_NAME
定位器策略。
参考下面的工作代码:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://twitter.com/i/flow/login")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
username = wait.until(EC.element_to_be_clickable((By.TAG_NAME, "input")))
username.send_keys('test')
time.sleep(10)
建议:使用 Selenium 的 waits 而不是
time.sleep()
。最后一行 time.sleep(10)
只是为了让您看到在页面上输入的文本,您不需要它。