【问题标题】:I can not find a way to click on a button using Python and Selenium我找不到使用 Python 和 Selenium 单击按钮的方法
【发布时间】:2018-10-03 02:56:59
【问题描述】:

我有一个问题,我碰巧有一个带有这些标签的按钮,我试图点击 Selenium,但我找不到点击它的方法。我试图将 XPath、链接文本和 CSS 选择器作为参考,但我没有达到我的目标。这是按钮的代码:

<a class="btn btn-flat pull-right" data-action="export_report"> <i class = "icon-export"> </ i> Export </a>

XPath 以东:

// * [@ id = "reports"] / div [1] / div [2] / a

这个选择器:

#reports> div.span12> div.headline-action-block.pull-right> a

这是按钮和我在 Python 中的代码 :(

按钮:

我的代码:

我遇到了这个错误

File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element
    'value': value})['value']

【问题讨论】:

  • 你试过我的答案了吗?
  • 是的,我认为该页面有一个机器人检测器,它没有给出我想要点击的元素的确切路径或 ID
  • 你有什么错误信息吗?

标签: python selenium xpath css-selectors webdriver


【解决方案1】:

你可以试试这个:

openrate = driver.find_element_by_css_selector("a.btn")
openrate.click()

假设这是唯一的按钮,或者是页面上的第一个按钮。否则("a.btn.btn-flat.pull-right")

【讨论】:

    【解决方案2】:

    根据您共享的 HTML,您似乎在上一步中调用了 click(),因此在此步骤中,在按钮上使用文本为 Exportclick(),您必须诱导 WebDriverwait 使元素可点击,您可以使用以下任一定位器策略

    • PARTIAL_LINK_TEXT

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      # lines of code     
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Export"))).click()
      
    • CSS_SELECTOR

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      # lines of code     
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-flat.pull-right[data-action='export_report']>i.icon-export"))).click()
      
    • XPATH

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      # lines of code     
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-flat pull-right' and @data-action='export_report']/i[@class='icon-export']"))).click()
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-22
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多