首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在python中使用selenium web驱动程序点击导航栏?

如何在python中使用selenium web驱动程序点击导航栏?
EN

Stack Overflow用户
提问于 2019-05-30 08:48:33
回答 1查看 951关注 0票数 1

使用office 365导航栏,我尝试单击用户图标进入另一个邮箱,但selenium无法通过id、class_name或xpath识别它。

代码语言:javascript
复制
elem6 = driver.find_element_by_xpath('//*[@id="O365_MeFlexPane_ButtonID"]')
elem6.send_keys(Keys.RETURN)

html标签

代码语言:javascript
复制
<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给出了以下结果

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2019-05-30 08:59:54

尝试使用以下代码添加预期条件以等待元素出现。

代码语言:javascript
复制
ele =WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,"//button[@id='O365_MeFlexPane_ButtonID']")))
ele.click()

如果你指的是下面的按钮

尝试使用下面的xpath。

代码语言:javascript
复制
//div[@id='meInitialsButton']

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

代码语言:javascript
复制
//div[@id='O365_MainLink_MePhoto']
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56370273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档