gpt4 book ai didi

python - 如何使用带有 python 的 selenium web 驱动程序单击导航栏?

转载 作者:太空宇宙 更新时间:2023-11-03 20:49:18 25 4
gpt4 key购买 nike

使用 Office 365 导航栏,我尝试单击用户图标进入另一个邮箱,但 selenium 无法通过 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()

如果您指的是下面的按钮

enter image description here

尝试使用下面的 xpath。

//div[@id='meInitialsButton']

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

//div[@id='O365_MainLink_MePhoto']

关于python - 如何使用带有 python 的 selenium web 驱动程序单击导航栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56370273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com