【问题标题】:Python Selenium click button with no name, no id, no text, many classes, and an aria-labelPython Selenium 没有名称、没有 id、没有文本、许多类和 aria-label 的单击按钮
【发布时间】:2021-04-29 00:39:02
【问题描述】:

这是按钮:

<button _ngcontent-qte-c250="" mat-raised-button="" color="primary" aria-label="Close Welcome Banner" class="mat-focus-indicator close-dialog mat-raised-button mat-button-base mat-primary ng-star-inserted" style=""><span class="mat-button-wrapper"><mat-icon _ngcontent-qte-c250="" role="img" class="mat-icon notranslate material-icons mat-icon-no-color" aria-hidden="true" data-mat-icon-type="font"> visibility_off </mat-icon><span _ngcontent-qte-c250="" fxshow="" fxhide.lt-sm="" translate="">Dismiss</span></span><span matripple="" class="mat-ripple mat-button-ripple"></span><span class="mat-button-focus-overlay"></span></button>

我什至怎么去点击这个? 尝试按照 this 链接所说的做,但 Xpath 似乎不喜欢使用许多类

尝试了以下方法,似乎没有任何效果。

driver.find_element_by_xpath("//div[@aria-label='Close Welcome Banner']/div[@class='mat-focus-indicator close-dialog mat-raised-button mat-button-base mat-primary ng-start-inserted']");
driver.find_element_by_xpath("//div[@aria-label='Close Welcome Banner']").click()
driver.findElement(By.xpath("//span[contains(@class,'middle') and contains(text(), 'Next')]"))

有什么建议吗?

【问题讨论】:

    标签: python selenium button click


    【解决方案1】:

    看起来像 button 标签,而不是 div

    试试这个:

    driver.find_element_by_xpath("//button[@aria-label='Close Welcome Banner']").click()
    

    【讨论】:

      【解决方案2】:

      你可以通过文本找到按钮标签的xpath。

      driver.find_element_by_xpath("//button[contains(text(), 'Dismiss')]")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-02
        • 1970-01-01
        • 1970-01-01
        • 2023-02-24
        • 1970-01-01
        • 1970-01-01
        • 2016-04-05
        • 1970-01-01
        相关资源
        最近更新 更多