【问题标题】:Selenium Automation – Interaction issueSelenium 自动化 – 交互问题
【发布时间】:2020-11-24 04:39:18
【问题描述】:

我正在开发一个能够登录网页的机器人 (postmark.com)。为此,我正在使用 selenium 和 python。现在我的代码能够访问网页,点击登录点击,插入用户名和密码;但是,当点击登录点击(访问帐户)时,我收到以下错误

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 webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.poshmark.com") #Open webpage
Log_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("xxx@xx.com")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Pass)).send_keys("123456")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(Log)).click()

有人知道为什么会这样吗? 谢谢

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:

    要将字符序列发送到用户名或电子邮件密码字段,您需要为WebDriverWait诱导element_to_be_clickable(),您可以使用以下任一种Locator Strategies:

    • 使用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("xxx@xx.com")
      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, "//a[text()='Log in']"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='login_form_username_email']"))).send_keys("xxx@xx.com")
      driver.find_element_by_xpath("//input[@id='login_form_password']").send_keys("123456")
      driver.find_element_by_xpath("//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) 减慢搜索诱导等待。


    参考

    您可以在以下位置找到一些相关讨论:

    【讨论】:

    • @Perro 很高兴能为您提供帮助。 Vote up questions and answers 您发现这对未来读者的利益很有帮助。见Why is voting important
    • 感谢您的回复。使用您的建议解决了该问题。但是我有一个关于最后一个对象“//button[@class='btn blue btn-primary']”的xpath的问题,你添加了一个不同的xpath。我使用检查器仔细检查了这一点,我得到的 xpath 是“//button[@class='btn btn--primary']”。介意告诉我你到了你家吗?
    • @Perro 我的错,在我发布答案后,我能够缩小到这个问题的范围。我错过了更新答案。您的 xpath 有点偏离。由于您使用了 xpathbtn btn--primary 不是确切的值,而是 btn blue btn-primary
    • 另一方面,我知道在Right Click -> Inspectctrl+shift+I 所描述的 HTML 中观察到了一些差异。我会建议后一种方法。请参阅Why XPath does not highlighted the yellow mark in Chrome84? 中的替代策略部分
    • 再次感谢您的回复和参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    相关资源
    最近更新 更多