【问题标题】:Click on HTML element using Python使用 Python 单击 HTML 元素
【发布时间】:2018-07-13 10:45:34
【问题描述】:

这是我在 HTML 上的元素:

<a aria-role="button" href="" class="sc-button-play playButton sc-button sc-button-xlarge" tabindex="0" title="Play" draggable="true">Play</a>

我使用 Selenium 来制作点击事件:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get(url)
buttons = driver.find_elements_by_class_name('playButton')

你可以猜到,它不起作用:)

【问题讨论】:

  • 用 xpath 试试
  • 我只看到人们使用元素的id,所以我不知道如何使用他的类
  • 就像@ThatBird 说的,试试xpath。如果您不熟悉它,请使用 chrome 插件“xPath Finder”。
  • 我在您的代码中没有看到对 click() 方法的任何调用。一旦你有了按钮,你会用它做什么?此外,当您说它不起作用时,您会收到什么错误/异常消息。这将为我们提供有关问题的线索。

标签: python html selenium selenium-webdriver


【解决方案1】:

用 xpath 试试 -

driver.find_elements_by_xpath("//a[@class = 'playButton']")

要找到xpath,您需要这样做 -

  • 右击你想要的元素,然后点击inspect

  • 已检查的元素将在 chrome 调试器中突出显示。右键单击该元素,将打开一堆选项

  • 点击copy,然后点击Copy XPath

并在上面的代码中使用该 xpath。

【讨论】:

    【解决方案2】:

    如果您想点击链接,请尝试

    driver.find_element_by_link_text("Play").click()
    

    如果链接文本在页面上实际显示为 PLAY ,请尝试

    driver.find_element_by_link_text("PLAY").click()
    

    【讨论】:

      【解决方案3】:

      试试这个:

      from selenium import webdriver
      
      driver = webdriver.Chrome()
      driver.get(url)
      # find all elements with following xPath (returns a list of elelements)
      buttons = driver.find_elements_by_xpath("//a[@class = 'playButton']") # using xPath
      

      或者如果你想点击一个元素使用这个:

      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      driver = webdriver.Chrome()
      driver.get(url)
      # wait(at least 10 seconds) for element will be clickable and clcick on it
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class = 'playButton']"))).click();
      

      Here你可以找到更多关于定位元素的信息。

      【讨论】:

      • 不区分大小写吗?播放按钮
      【解决方案4】:

      @Andersson @AndreiSuvorkov 和 @ThatBird 给出了答案,但似乎还有更多因素需要我们考虑如下:

      当您调用 get(url) 并在下一步尝试在元素上调用 click() 时,

      • 您需要使用 find_element* 而不是 find_elements*,如下所示:

        button = driver.find_element_by_class_name('class_name')
        
      • 在调用 click 之前,您需要诱导 WebDriverWait 以使 元素可点击,如下所示:

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "element_xpath"))).click();
        
      • 当您希望单击特定元素时,请借助 Locator Strategy,它将唯一标识 DOM Tree 中的 WebElement。对于&lt;a&gt; 节点(即锚标签)LINK_TEXTPARTIAL_LINK_TEXT 必须是首选选项。除了这些之外,一种非常传统的方法是广泛使用classid 属性(没有classid 属性回退到其他属性)来构造一个CssSelector 或XPath如下:

      • LINK_TEXT:

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Play"))).click()
        
      • CSS_SELECTOR:

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.sc-button-play.playButton.sc-button.sc-button-xlarge[title='Play']"))).click()
        
      • XPATH:

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='sc-button-play playButton sc-button sc-button-xlarge' and @title='Play'][contains(.,'Play')]"))).click();
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-27
        • 1970-01-01
        • 2019-05-18
        • 1970-01-01
        • 2021-03-28
        • 2018-05-26
        • 2021-03-31
        • 1970-01-01
        相关资源
        最近更新 更多