嘗試使用 selenium 處理 instagram 自動化頁面上的用戶名輸入欄位時,我得到了意外的結果。
代碼:
from selenium import webdriver
from selenium.webdriver.common.by import By
patch = r'{patch to driver}'
driver = webdriver.Chrome(patch)
driver.get('https://api.instagram.com/oauth/authorize?client_id=965975207687609&redirect_uri=https://6b4c-46-39-51-57.ngrok.io/autos/index.php/&scope=user_profile,user_media&response_type=code')
elem = driver.find_element(By.NAME, 'username')
頁面驅動重定向到:https ://www.instagram.com/accounts/login/?force_authentication=1&enable_fb_login=1&next=/oauth/authorize?client_id=965975207687609&redirect_uri=https://6b4c-46-39 -51-57.ngrok.io/autos/index.php/&scope=user_profile,user_media&response_type=code 并且此頁面上肯定有元素名稱“用戶名”,但我收到一堆錯誤訊息:
C:\Users\digit\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\gitdir\api\insta_api.py:54: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome(patch)
DevTools listening on ws://127.0.0.1:58299/devtools/browser/8fd4d931-7cc4-4070-b3f6-3a6a0df9769e Traceback (most recent call last): File "C:\Users\digit\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\gitdir\api\insta_api.py", line 64, in <module>
elem = driver.find_element(By.NAME, 'username') File "C:\Users\digit\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, { File "C:\Users\digit\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
self.error_handler.check_response(response) File "C:\Users\digit\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="username"]"} (Session info: chrome=97.0.4692.71)
uj5u.com熱心網友回復:
Instagram 登錄頁面上的用戶名欄位是一個ReactJS元素。因此,要發送一個字符序列,您必須為element_to_be_clickable()誘導WebDriverWait并且您可以使用以下任一定位器策略:
使用CSS_SELECTOR:
driver.get('https://api.instagram.com/oauth/authorize?client_id=965975207687609&redirect_uri=https://6b4c-46-39-51-57.ngrok.io/autos/index.php/&scope=user_profile,user_media&response_type=code') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("Ignat")使用XPATH:
driver.get('https://api.instagram.com/oauth/authorize?client_id=965975207687609&redirect_uri=https://6b4c-46-39-51-57.ngrok.io/autos/index.php/&scope=user_profile,user_media&response_type=code') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']"))).send_keys("Ignat")
注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418971.html
標籤:
上一篇:如何抑制博覽會中的特定警告
