Reputation: 57
I am on a page in which I need to click on a variable's logo/name/arrow (3 button options). The variable is called HISCO occupation. HTML from 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>
My entire code so far:
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()
As of now, I have the following (unsuccessful) code trying to click on either of these buttons:
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="workhistoryunit_category"]/button[1]'))).click()
or
webD.find_element_by_xpath('//*[@id="workhistoryunit_category"]/button[1]').click()
The action prior to clicking this button is closing a window (applying a selection).
This code is based on the code from a previous analogous click on the same page, just another variable (hence different ID) that works:
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="county_category"]/button[1]'))).click()
Any ideas what the problem maybe?
Upvotes: 0
Views: 685
Reputation: 19929
# 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()
Try this there is small tool tip being displayed for few seconds that is removing the reference , so use this code first to check its presence then its invisibility and then click that button
There is a green tool tip that comes up same time the tab you want to click is loaded , so it resets the element reference and causes stale element exception. You need to wait for that elemnt to disappear first. But as the element is displayed only for few seconds we cannot find its locator . To find its locator open inspect and right click the html element and add break on > subtree modification and keep resuming execution still you get that tool tip
Upvotes: 1
Reputation: 193088
To click on the element with text as HISCO OCCUPATION you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
Using 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()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Upvotes: 0
Reputation: 33384
Use following XPath
to click on the button.
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[./span[text()='HISCO classified occupation']]"))).click()
Upvotes: 2