Selenium Automation使用Python和Selenium在ul中选择li菜单项

2024-05-23 23:32:47 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我遇到了一个总体问题,我试图自动登录、菜单项选择,然后预填充字段以初始化文件下载。我已经能够自动登录,非常感谢在另一篇文章中提供的答案。但是,我没有成功地尝试导航到特定的菜单项并选择该项以进入新页面。如果我直接进入该页面,我已注销,必须重新登录。这里有一个名为“自定义报告”的站点,这是我需要单击以转到另一个页面的位置。如果li对象有ID,这将使这变得容易得多,但是没有可以想象的ID。这是我试图自动生成的网页的源代码,请按照我存储在github中的链接进行操作

https://github.com/Richard-Barrett/ITDataServicesInfra/blob/master/Python/Collegeboard/TSI/tsi_pagesource

我正在尝试单击此元素

                  <li ng-show="core.acl['244']!='N'" ng-class="{active:isLocation('/customReports')}">
                     <a href="#/customReports" translate='reports.CustomReports.title'></a>
                  </li>

正如你所能想象的那样,我一直未能做到这一点。 这是我目前用来提取信息的代码。登录自动化工作

#!/bin/python
# ===========================================================
# Created By: Richard Barrett
# Organization: DVISD
# DepartmenT: Data Services
# Purpose: Test Score & 3rd Party Website Data Pull Automation
# Date: 01/20/2020
# ===========================================================

import selenium
import os
import unittest
import requests
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select

#Variable Definitions NEED TO MAKE
#find_elements_by_name
#find_elements_by_xpath
#find_elements_by_link_text
#find_elements_by_partial_link_text
#find_elements_by_tag_name
#find_elements_by_class_name
#find_elements_by_css_selector


#URL Variables
login_url = 'https://www.accuplacer.org/'
redirect_url = 'https://www.accuplacer.org/api/home.html#/'
reports_scheduler_url = 'https://www.accuplacer.org/api/home.html#/reportScheduler'
custom_reports_url = 'https://www.accuplacer.org/api/home.html#/customReports'

#WebDriver Path
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")

#Parent URL
browser.get("https://www.accuplacer.org")

# Authentication
# Credentials NEEDS TO BE ENCRYPTED AND NOT BAKED INTO THE SCRIPT NEEDS UNIT TEST
username = browser.find_element_by_id("login")
password = browser.find_element_by_id("password")
username.send_keys("##############")
password.send_keys("##############")
#browser.send_keys(Keys.ENTER)
#browser.send_keys(Keys.RETURN)

# Work on Notifications Window Clause
# Need a step for when notifications appear to click "Submit" option
# <button type="button" ng-show="updateWhatsNewFlag == 'failure'" translate="submit" class="btn btn-sm btn-success ng-scope" ng-click="updateShowWhatsNewFlag()" style="">Submit</button>

# Need a step to close if the notification popup window is present
# <button type="button" class="btn btn-default ng-scope" data-dismiss="modal" ng-click="clearMessages()" translate="common.btn.close">Close</button>

# Authentication submit.click()
#browser.find_element_by_css_selector('.btn.btn-lg.btn-primary').click()
element = WebDriverWait(browser, 20).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.btn-lg.btn-primary.pull-left")))
element.click();

#Navigate to CustomReports XPATH=//*[@id="leftNav"]/ul/li[11]/ul/li[9]/a
#browser.find_element_by_xpath('//*[@id="leftNav"]/ul/li[11]').click()


# Make the report by selecting objects
#obj_report = Select(browser.find_element_by_name("Reports"))
element = WebDriverWait(browser, 20).until(
        EC.element_to_be_clickable((By.CSS_XPATH, "*[@id="leftNav"]/ul/li[11]/ul/li[9]/a")))
element.click();

每次我尝试使用xpath时,它都不会给我任何东西,基本上说它不存在


Tags: fromhttpsimportbrowserbyseleniumbuttonli
3条回答

所需的元素是Angular元素,因此要在元素上定位和click(),您需要为element_to_be_clickable()诱导WebDriverWait,并且可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li>a[href$='customReports'][translate*='CustomReports']"))).click()
    
  • 使用XPATH

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//li/a[contains(@href,'customReports') and contains(@translate, 'CustomReports')]"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

请使用xpath和webdriver explicit wait尝试下面的代码

element = WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, '/customReports')]")))
element.click();

你需要在下面输入

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

不推荐,但在大多数情况下都是可行的解决方案(尝试使用Java脚本单击,但不要等待,因为等待失败)

element=browser.find_element(By.XPATH, "//a[contains(@href, '/customReports')]")
browser.execute_script("arguments[0].click();", element)

所以我最终找到了解决办法。解决方案是,我必须首先导航到报表,然后移动到自定义报表的子项。我用Link_文本代替

element = WebDriverWait(browser, 20).until(
        EC.element_to_be_clickable((By.LINK_TEXT, "Reports")))
element.click();

element = WebDriverWait(browser, 20).until(
                EC.element_to_be_clickable((By.LINK_TEXT, "Custom Reports")))
element.click();

我感谢你们这些@DebanjB和@Muzzamil的帮助,我怀疑如果没有他们的指导,我将无法找到如何做到这一点!非常感谢你们两位

相关问题 更多 >