簡體   English   中英

如何使用Selenium Web驅動程序和python單擊導航欄?

[英]How to click on navigation bar using selenium web driver with python?

使用office 365導航欄,我嘗試單擊用戶圖標以輸入另一個郵箱,但是硒無法通過id,class_name或xpath識別它?

elem6 = driver.find_element_by_xpath('//*[@id="O365_MeFlexPane_ButtonID"]')
elem6.send_keys(Keys.RETURN)

html標簽

<button type="button" class="o365cs-nav-item o365cs-nav-button o365button ms-bgc-nlr ms-fcl-b o365cs-spo-topbarMenuOpen" role="menuitem" title="Carlos Loayza Cuestas" id="O365_MeFlexPane_ButtonID" aria-disabled="false" aria-label="Use la flecha hacia abajo para utilizar el panel Mi cuenta para ver y editar su cuenta y cerrar la sesión"><div class="o365cs-mfp-header"> <div class="o365cs-mfp-header-displayname o365cs-rsp-tn-hide o365cs-mfp-header-displayname-wrap o365cs-display-none"> <span class="" role="presentation">Carlos Loayza Cuestas</span> </div> <div class="o365cs-nav-button o365cs-mfp-header-img"> <div class="o365cs-mfp-doughboy-container"> <span class="ms-bgc-nt ms-fcl-w o365cs-mfp-doughboy o365cs-mfp-circular-small owaimg ms-Icon ms-Icon--Contact ms-icon-font-size-52" aria-hidden="true"> </span> </div> <div class="o365cs-mfp-textboy o365cs-mfp-circular-small" style=""><div class="" style=""><div style="background-color: rgb(118, 118, 118); outline-color: rgb(118, 118, 118);">CC</div></div></div> <div style="display: none;"></div> <div class="o365cs-mfp-header-img-container o365cs-mfp-circular-small" style="display: none;"><img aria-hidden="true" src="service.svc/s/GetPersonaPhoto?email=carlos.loayza@telefonica.com&amp;UA=0&amp;size=HR64x64&amp;sc=1559170428032"></div> <div autoid="__Microsoft_O365_ShellG2_MeFlexHeaderButton_Owa_templates_cs_0" class="o365cs-mfp-presenceButton ms-Icon--skypeCheck o365cs-mfp-skypeAvailable"></div> </div> </div><div class="o365cs-flexPane-unseenitems"> <span class="o365cs-flexPane-unseenCount ms-fcl-w ms-bgc-tdr" style="display: none;"></span> <span class="o365cs-flexPane-unseenCount owaimg ms-Icon--starburst ms-icon-font-size-12 ms-fcl-w ms-bgc-tdr" style="display: none;"> </span> </div></button>

python給我以下結果

Traceback (most recent call last):
  File "C:\Users\wcondorp\Videos\Selenium\prueba.py", line 80, in <module>
    elem6 = driver.find_element_by_xpath('//*[@id="O365_MeFlexPane_ButtonID"]') #mail, onedrive, calendaria
  File "C:\Users\wcondorp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\wcondorp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\wcondorp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\wcondorp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="O365_MeFlexPane_ButtonID"]"}
  (Session info: chrome=74.0.3729.169)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17134 x86_64)

嘗試使用以下代碼添加期望條件以等待元素出現。

ele =WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,"//button[@id='O365_MeFlexPane_ButtonID']")))
ele.click()

如果您指的是以下按鈕

在此處輸入圖片說明

請嘗試使用以下xpath。

//div[@id='meInitialsButton']

或者,您也可以使用以下xpath。

//div[@id='O365_MainLink_MePhoto']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM