我是 python 和 Selenium 的初學者,我正在嘗試創建一個基本代碼來為我自動登錄到一個站點。
https://www.faceit.com/en/login
在我在網上找到的教程中,ID 可以更輕松地識別元素,但是,上面的頁面沒有 ID,因此我求助于使用 XPATH 作為識別元素的方法。
我的 XPATH 格式或代碼中的某些內容是否有問題導致在欄位中根本不列印我的文本并隨后不登錄我的問題?
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.faceit.com/en/login")
email_textbox = driver.find_element_by_xpath("//input[@type='email']")
email_textbox.send_keys("emailx")
password_textbox = driver.find_element_by_xpath("//input[@type='password']")
password_textbox.send_keys("passwordx")
login_button = driver.find_element_by_xpath("//button[@type='submit']")
login_button.click()
uj5u.com熱心網友回復:
可能您必須等到您的元素就位。嘗試 time.sleep(Some kind of time) 或嘗試使用檔案中的功能之一:) https://selenium-python.readthedocs.io/waits.html
解決方案:在瀏覽器中打開開發者工具,有一個選項按鈕“選擇頁面中的一個元素來檢查它”insepct 你想要的元素,在開發工具中單擊右鍵并復制 Xpath :)
如果這有助于投票給我!:)
uj5u.com熱心網友回復:
您在腳本中使用的 XPath 是正確且良好的,但看起來元素未在默認等待時間內加載。所以在這里你可以在找到這些元素之前添加一些等待時間。同樣要驗證和生成 xpath,您可以使用 SelectorsHub 瀏覽器插件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/376415.html
