亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Selenium Automation – 交互問題

Selenium Automation – 交互問題

守候你守候我 2023-04-25 16:54:46
我正在開發一個能夠登錄網頁 ( postmark.com) 的機器人。為此,我使用了 selenium 和 python?,F在我的代碼能夠訪問網頁,點擊登錄按鈕,輸入用戶名和密碼;然而,當點擊登錄點擊(訪問帳戶)時,我收到以下錯誤Traceback (most recent call last):  File "/home/pi/Documents/Bot_Poshmark.py", line 20, in <module>    WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Log)).click()  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/support/wait.py", line 80, in until    raise TimeoutException(message, screen, stacktrace)selenium.common.exceptions.TimeoutException: Message:奇怪的是,有時(比如 2 或 3)我編寫的同一段代碼可以完成所有步驟。這是我的代碼(我為此使用 Raspberry Pi 4)from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()driver.get("https://www.poshmark.com") #Open webpageLog_Field=(By.XPATH, "//a[contains(text(),'Log in')]")Email= (By.XPATH, "//input[@placeholder='Username or Email']")Pass= (By.XPATH, "//input[@placeholder='Password']")Log= (By.XPATH, "//button[@class='btn btn--primary']")WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Log_Field)).click()WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Email)).send_keys("[email protected]")WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Pass)).send_keys("123456")WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Log)).click()有誰知道為什么會這樣?謝謝
查看完整描述

1 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

要將字符序列發送到用戶名或電子郵件密碼?字段,您需要引入WebDriverWait并且element_to_be_clickable()您可以使用以下任一定位器策略:

使用CSS_SELECTOR:


driver.get("https://poshmark.com/")

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='/login']"))).click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#login_form_username_email"))).send_keys("[email protected]")

driver.find_element_by_css_selector("input#login_form_password").send_keys("123456")

driver.find_element_by_css_selector("button.btn.blue.btn-primary").click()

使用XPATH:


driver.get("https://poshmark.com/")

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "http://a[text()='Log in']"))).click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "http://input[@id='login_form_username_email']"))).send_keys("[email protected]")

driver.find_element_by_xpath("http://input[@id='login_form_password']").send_keys("123456")

driver.find_element_by_xpath("http://button[@class='btn blue btn-primary']").click()

注意:您必須添加以下導入:


from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC

這個用例

在這個用例中,行:

WebDriverWait(driver,?20).until(EC.element_to_be_clickable(Log)).click()

無法在所需的時間范圍內識別所需的元素,因此您遇到了TimeoutException。

然而,從TimeoutException中很難挖掘出失敗的實際結果。


解決方案

作為了解失敗確切原因的解決方案,您需要刪除 WebDriverWait并將代碼行替換為:

  • find_element_by_class_name(name)

  • find_element_by_css_selector(css_selector)

  • find_element_by_id(id)

  • find_element_by_link_text(link_text)

  • find_element_by_name(name)

  • find_element_by_partial_link_text(partial_link_text)

  • find_element_by_tag_name(tag_name)

  • find_element_by_xpath(xpath)

如果需要,您可以在調試時減慢搜索誘導等待的速度time.sleep(secs)。


查看完整回答
反對 回復 2023-04-25
  • 1 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號