【问题标题】:Selenium - Issue Getting/Clicking Button ElementSelenium - 问题获取/单击按钮元素
【发布时间】:2021-03-29 16:12:54
【问题描述】:

我一直在使用 python 和 selenium,并试图制作一个可以在 bestbuy.ca 上自动结帐的程序。我已经接近结帐过程的最后阶段,但很难获取元素/单击按钮继续。我能够开始工作的唯一按钮是产品页面上的添加到购物车按钮。

这是网站和 HTML:

Click here for image

我尝试过但没有成功的一些事情包括:

driver.find_element_by_class_name().click()

  • 为此,我使用了按钮和跨度中的类。我曾尝试单独和一起使用每个单独的课程,但收效甚微。它通常会给我一个“未找到元素”错误,或者之后不点击任何内容。

driver.driver.find_element_by_xpath().click()

  • 为此,我尝试使用 xpath 的元素定位器扩展以及“//*[@type='submit']”,但没有成功。

driver.driver.find_element_by_css_selector().click()

  • 为此,我再次尝试了元素定位器扩展,并从 Firefox 检查中复制它,但没有成功。

不确定是否还有其他方法可以做到这一点,但我们将不胜感激。

【问题讨论】:

  • 您好!解决此问题的一种流行方法是在 python selenium 中使用Wait。您可以在此处查看并将其应用于您的代码:) link

标签: python html selenium


【解决方案1】:

尝试以下任一方法来点击按钮标签。

wait=WebDriverWait(driver, 10)

wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Continue']/parent::button"))).click()

或者通过属性数据自动化

wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@data-automation='review-order']"))).click()

导入

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

【讨论】:

    【解决方案2】:

    您是否尝试从 html 中复制整个类属性并使用:

    wait.until(EC.presence_of_element_located((By.XPATH, "//button[@class='asd asd sad asd']"))).click()
    

    复制类并粘贴它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多