Selenium:很难点击页面上的第二个按钮

Selenium: struggle clicking a second button on a page

我在一个页面上需要单击变量的 logo/name/arrow(3 个按钮选项)。该变量称为 HISCO 职业。 HTML 来自 URL:https://icem.data-archive.ac.uk/#step2

<div class="clearfix ice-tab-wrapper" id="workhistoryunit_category" ng-class="{active:tabActive[child.name]}">
:: before
<button class="fa fa-certificate ice-icon" ng-click="toggleTab(child.name, $event)">
:: before
<span class= "sr-only ng-binding"> HISCO OCCUPATION </span>
</button>
< button .....> </button>

到目前为止我的全部代码:

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

chrome_path = r'C:\webdrivers\chromedriver.exe'

webD=wd.Chrome(executable_path=chrome_path)
webD.get('https://icem.data-archive.ac.uk/#step1')


## STEP 1: SELECTING A YEAR, HERE 1851

webD.find_element_by_xpath('/html/body/div/section/section[1]/article[1]/div[1]/div/div[1]/label/input').click()


## STEP 2: SELECTING ENGLAND

WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '//b[@id = "country_england"]/preceding-sibling::input'))).click()


## STEP 3: MOVE ON TO VARIABLES

webD.find_element_by_xpath('//html/body/div/section/section[1]/article[2]/div/div/button').click()


## STEP 4.1: COUNTIES, OPEN MENU

WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '//*[@id="county_category"]/button[1]'))).click()


## STEP 4.2 COUNTIES, MORE VARIABLES
WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '//*[@id="county_category"]/div[2]/div/div[2]/button'))).click()

## VERSION 1, SELECTION 1 COUNTY/ 1 HISCO_OCC
## STEP 4.3 COUNTIES, SELECT VARIABLES

WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '/html/body/div[3]/div/div/div[3]/div[1]/label/input'))).click()

## STEP 4.3.2 CLICK APPLY

WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Apply')]"))).click()


# STEP 5, HISCO OCCUPATION, OPEN MENU

WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.ID, "workhistoryunit"))).click()

截至目前,我有以下(不成功的)代码试图点击这些按钮中的任何一个:

WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="workhistoryunit_category"]/button[1]'))).click()

webD.find_element_by_xpath('//*[@id="workhistoryunit_category"]/button[1]').click()

单击此按钮之前的操作是关闭 window(应用选择)。

此代码基于同一页面上一次类似点击的代码,只是另一个起作用的变量(因此 ID 不同):

WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="county_category"]/button[1]'))).click()

有什么问题吗?

要单击文本为 HISCO OCCUPATION 的元素,您需要诱导 WebDriverWait for the and you can use either of the following :

  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ice-tab-button ng-binding']/b[@class='ng-binding' and contains(., 'HISCO classified occupation')]"))).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点击按钮。

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[./span[text()='HISCO classified occupation']]"))).click()
# STEP 5, HISCO OCCUPATION, OPEN MENU


WebDriverWait(webD, 20).until(
    EC.presence_of_element_located((By.XPATH, '//span[@class = "ice-allow-download-alert ng-animate ng-hide-remove ng-hide-remove-active"]')))

WebDriverWait(webD, 20).until(
    EC.invisibility_of_element_located((By.XPATH, '//span[@class = "ice-allow-download-alert ng-animate ng-hide-remove ng-hide-remove-active"]')))

WebDriverWait(webD, 20).until(
    EC.presence_of_element_located((By.XPATH, '//b[text()[contains(.,"HISCO classified occupation")]]/..'))).click()

试试这个,会显示几秒钟的小工具提示正在删除引用,因此请先使用此代码检查其存在,然后检查其不可见性,然后单击该按钮

在加载要单击的选项卡的同时会出现一个绿色工具提示,因此它会重置元素引用并导致陈旧元素异常。您需要等待那个元素先消失。但是由于该元素仅显示几秒钟,我们无法找到它的定位器。要找到它的定位器,打开检查并右键单击 html 元素并添加 break on > subtree modification and keep resuming execution still you get that tool tip