skip to Main Content

I’m struggling to click this html element/locate it using selenium in python on this website:

https://qualifications.pearson.com/en/support/support-topics/exams/past-papers.html?Qualification-Family=International-Advanced-Level

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


  1. Try the below XPath expression to locate "Biology (2018)":

    xpath = //div[@class='modal-cover']//h3[text()='Biology (2018)']
    

    And this one for "Biology":

    xpath = //div[@class='modal-cover']//h3[text()='Biology']
    

    Full working code for your reference:

    import time
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome()
    driver.maximize_window()
    wait = WebDriverWait(driver, 30)
    driver.get("https://qualifications.pearson.com/en/support/support-topics/exams/past-papers.html?Qualification-Family=International-Advanced-Level")
    # Accept Cookies
    wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
    # Click on element B
    wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='B']"))).click()
    # Click on element Biology
    wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Biology']"))).click()
    # Click on element Biology (2018) in the pop-up
    wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='modal-cover']//h3[text()='Biology (2018)']"))).click()
    time.sleep(20)
    

    Result:
    enter image description here

    Login or Signup to reply.
  2. Given the HTML:

    <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>
    

    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:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='modal-cover']//h3[@class='ng-binding' and text()='Biology (2018)']"))).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
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search