【问题标题】:click button type='button element with selenium webdriver in class using python在使用python的类中单击带有selenium webdriver的按钮类型='按钮元素
【发布时间】:2018-03-14 10:35:21
【问题描述】:

enter image description here

我试图用 python selenium 整天点击这个,但没有运气,尝试了几个选择器,xpath ..似乎对我没有用。这是我尝试点击的元素: 我需要点击 sharepoint.com 上的按钮

代码

log_but2 = "//button[@class='o365cs-nav-item o365cs-nav-button o365button ms-bgc-tdr-h o365cs-topnavText' and role='menuitem']"
driver.find_element_by_xpath(log_but2).click()

失败了

问题:我需要从 sharepoint.com 打开 onedrive(附图片)

HTML 代码:

<button type="button" class="o365cs-nav-item o365cs-nav-button o365button ms-bgc-tdr-h o365cs-topnavText" role="menuitem" title="Open the app launcher to access Office 365 apps" id="O365_MainLink_NavMenu_Responsive" aria-disabled="false" aria-selected="false" aria-label="App launcher">
<span class="owaimg ms-Icon ms-Icon--Waffle ms-icon-font-size-24 o365cs-topnavText" aria-hidden="true">
</span><div class="o365cs-flexPane-unseenitems"> 
<span class="o365cs-flexPane-unseenCount ms-bgc-tdr ms-fcl-w" style="display: none;">
</span> <span class="o365cs-flexPane-unseenCount owaimg ms-Icon--starburst ms-icon-font-size-12 ms-bgc-tdr ms-fcl-w" style="display: none;"> 
</span> </div></button>

【问题讨论】:

  • 欢迎来到 Stack Overflow!请向我们展示您的尝试。你可以edit它进入你的问题。如果可能,请包含minimal reproducible example。这有助于我们更好地理解您的问题,并提高您获得有用答案的机会。我只能看到导入。
  • 1- 将 html 作为文本发布,而不是图像 2- 分享失败的代码 3- 解释导致失败的原因(如果您也有异常发布堆栈跟踪)。

标签: python selenium xpath selenium-webdriver webdriver


【解决方案1】:

根据您分享的HTML,点击带有文本的按钮作为应用启动器,您需要诱导WebDriverWait 如下:

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

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='o365cs-nav-item o365cs-nav-button o365button ms-bgc-tdr-h o365cs-topnavText' and @id='O365_MainLink_NavMenu_Responsive']"))).click()

【讨论】:

  • 我需要点击 href 所以:-
  • @sanjanakapoor 我没有收到你的问题。 HTML 本身不包含任何href
【解决方案2】:

作为在黑暗中的盲目刺,试试这个:

driver.find_element_by_xpath("//*[contains(@class, 'ms-Icon')][contains(text(), 'OneDrive')]")

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 2020-09-04
    • 2019-02-27
    • 1970-01-01
    • 2017-01-17
    相关资源
    最近更新 更多