我正在尝试通过单击右上角的图片来完成我的脚本,这会导致注销按钮所在的下拉菜单。我可以让 Selenium 识别图片并单击图片以显示下拉菜单,但我无法让它单击最后一个按钮。这是我试图单击的注销按钮的网站代码:<a id="ctl00_ctl00_mainContent_MainHeader_HeaderSection_lnkLogout" href="javascript:__doPostBack('ctl00$ctl00$mainContent$MainHeader$HeaderSection$lnkLogout','')"><i class="fa fa-power-off fa-fw"></i> <span>Log Out</span> </a>这是整个下拉列表的完整代码。看起来这是一个我第一次没有意识到的列表,但我记得读过你可以按数字选择一个列表?<div class="dropdown header-container user-actions-container open"> <a id="ctl00_ctl00_mainContent_MainHeader_HeaderSection_hypPicture" class="employee-picture" role="button" data-toggle="dropdown" aria-expanded="true"><span id="ctl00_ctl00_mainContent_MainHeader_HeaderSection_imgPicture" alternatetext="Employee Picture" imagealign="Top"><img class="img-rounded" src="https://share.striven.com//pimg/BA4FE360/Employee/thumb-b8598be5-5cd3-48c3-86ab-79fc9985b95e-07252018.jpg"></span></a> <ul class="dropdown-menu dropdown-menu-right"> <li> <a id="ctl00_ctl00_mainContent_MainHeader_HeaderSection_rptEmployeeActions_ctl00_lnkAction" href="/HR/Appointments/AppointmentsCalendar.aspx?nav=1"><i class="fa fa-calendar fa-fw"></i> <span>Calendar</span> </a></li>我试图模仿有人在这里帮助我完成几乎相同的事情的这段代码:element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.hub-content-item[id*='SearchExplorer'][actiontype='Secondary']>span")))但它不会为我识别它。
1 回答

慕运维8079593
TA贡献1876条经验 获得超5个赞
你非常接近。该<span>标记不是直接子级,因此您需要>用空格字符替换,并且可以使用以下任一定位器策略:
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[id$='mainContent_MainHeader_HeaderSection_lnkLogout'] span"))).click()
使用XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@id, 'mainContent_MainHeader_HeaderSection_lnkLogout')]//span[text()='Log Out']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
添加回答
举报
0/150
提交
取消