使用python在类中单击按钮type ='button元素和selenium webdriver

时间:2018-03-14 10:35:22

标签: python selenium xpath selenium-webdriver webdriver

enter image description here

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

CODE

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 CODE:

<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>

2 个答案:

答案 0 :(得分:0)

在黑暗中盲目刺伤,试试这个:

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

答案 1 :(得分:0)

根据您共享的 HTML 点击按钮,文字为应用启动器,您需要诱导 WebDriverWait < / em>如下:

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()