I’m struggling to click this html element/locate it using selenium in python on this website:
After you scroll down press B and then Biology the pop up menu shown in the attached screenshot with "Biology (2018)" and "Biology" buttons are the desired elements and here is the html code for the "Biology (2018)" button:
<li data-ng-repeat="modalSubject in subject.subjectsList | orderBy:'-title'" class="ng-scope">
<a tabindex="0" data-ng-keypress="handleKeyPress($event)" data-ng-click="setExamSeriesUrl(modalSubject);">
<h3 class="ng-binding">Biology (2018)</h3>
<div class="description ng-binding"></div>
</a>
</li>
How do I locate this element and click it?
I tried this to locate and click the element but to no avail with a timeout error(despite me using time.sleep()
to give it a long enough lag for the element to appear/show up):
xpath = '//li[contains(.//h3, "{}")]'
formatted_xpath = xpath.format(Subject+" (2018)")
element = wait.until(EC.visibility_of_element_located((By.XPATH, formatted_xpath)))
element.click()
2
Answers
Try the below XPath expression to locate "Biology (2018)":
And this one for "Biology":
Full working code for your reference:
Result:
Given the HTML:
The element with text Biology (2018) is an Angular element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:
Using XPATH:
Note: You have to add the following imports :