在Python中单击带有Selenium的下拉菜单

时间:2019-04-22 14:55:15

标签: python selenium xpath css-selectors webdriverwait

我正在尝试通过单击右上角的图片来结束脚本,这会导致退出菜单位于退出按钮的位置。我可以让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>

                                    <li>
                                        <a id="ctl00_ctl00_mainContent_MainHeader_HeaderSection_rptEmployeeActions_ctl01_lnkAction" href="/Finance/Reimbursements/ReimbursementsMyReimbursements.aspx?nav=1"><i class="fa fa-money  fa-fw"></i>                                                           
                                          <span>Expenses</span>  

                                        </a></li>

                                    <li>
                                        <a id="ctl00_ctl00_mainContent_MainHeader_HeaderSection_rptEmployeeActions_ctl02_lnkAction" href="/HR/ReportBuilder/MyReports.aspx?nav=1"><i class="fa fa-bar-chart   fa-fw"></i>                                                           
                                          <span>Reports</span>  

                                        </a></li>

                                    <li>
                                        <a id="ctl00_ctl00_mainContent_MainHeader_HeaderSection_rptEmployeeActions_ctl03_lnkAction" href="/AssetManagement/MyAssets/DialogMyAssetList.aspx?nav=1"><i class="fa fa-book   fa-fw"></i>                                                           
                                          <span>Reserved Assets</span>  

                                        </a></li>

                                    <li>
                                        <a id="ctl00_ctl00_mainContent_MainHeader_HeaderSection_rptEmployeeActions_ctl04_lnkAction" href="/Collaboration/DialogMySubscriptions.aspx?nav=1"><i class="fa fa-envelope-o  fa-fw"></i>                                                           
                                          <span>Subscriptions</span>  

                                        </a></li>

                                    <li>
                                        <a id="ctl00_ctl00_mainContent_MainHeader_HeaderSection_rptEmployeeActions_ctl05_lnkAction" href="/HR/Timeoff/EmployeeTimeoff.aspx?nav=1"><i class="fa fa-calendar-times-o  fa-fw"></i>                                                           
                                          <span>Time Off</span>  

                                        </a></li>

                                    <li>
                                        <a id="ctl00_ctl00_mainContent_MainHeader_HeaderSection_rptEmployeeActions_ctl06_lnkAction" href="/HR/TimeAndMaterial/ManageTimeEntry.aspx?nav=1"><i class="fa fa-clock-o  fa-fw"></i>                                                           
                                          <span>Timesheet</span>  

                            <li>
                                <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></li>
                        </ul>
                </div>

我试图模仿这段代码,有人在这里帮我完成了几乎相同的事情:

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.hub-content-item[id*='SearchExplorer'][actiontype='Secondary']>span")))

但它不会为我识别它。

1 个答案:

答案 0 :(得分:2)

您非常接近。 <span>标签不是立即子,因此您需要将 > 替换为 space 字符,然后使用以下Locator Strategies之一:

  • 使用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