0

I have the keyword "Know More" (using the keyword I find Xpath of that and performing actions like click) and the website has similar words like "Know More" for students loan, educational loan...etc which are not buttons but they are anchor tags. Now I want to click the "Know More" keyword using XPATH which is for "student loan" but student loan is available in block.

<div class="content">
    <h5 class="mb-10">For Students</h5>
    <h3>Enabling a hassle-free learning journey with curated solutions</h3>
    <h4>#CampusPower #StudentLoan</h4>
    <ul class="bullet-list bullet-list-orange mb-20">
        <li>One stop solution for all your education needs</li>
        <li>Customised product bundles for the country of your choice</li>
        <li>Explore pre & post admission guidance</li>
        <li>Learn how to manage your money.</li>
        </ul>
    <a id="ga-ese-whatsnew-1" href="/campus-power/student" class="ic-btn ic-btn-gradient">Know More</a>
    </div>

I don't want to take manual Xpath so I used code based on Keyword Xpath lik this.

element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, f"//*[contains(text(), '{keyword}')]"))
        )
element.click()

It is clicking only the first "Know More" on the webpage which is for another loan. How to click on "Know More" for student loan any help on suggestion is appreciated. Thank You.

2 Answers2

0

To click on Know More link within the For Students section, you can locate the element with text For Students first and then with reference to that WebElement you can traverse the HTML DOM to locate the required link inducing WebDriverWait for the element_to_be_clickable() using either of the following locator strategies:

  • Using XPATH and following-sibling:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h5[text()='For Students']//following-sibling::a[1]"))).click()
    
  • Using XPATH and following:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h5[text()='For Students']//following::a[1]"))).click()
    
  • Using XPATH and following-sibling and the text Know More:

    keyword = "Know More"
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h5[text()='For Students']//following-sibling::a[text()='{keyword}']"))).click()
    
  • Using XPATH and following and the text Know More:

    keyword = "Know More"
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h5[text()='For Students']//following::a[text()='{keyword}']"))).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
    

Update

As an alternative you can also use the following locator strategy:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[text()='One stop solution for all your education needs']//following::a[text()='Know More']"))).click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I shouldn't go through "inspect elements" and find the code. As per the given instructions I need to pass the "Know More" keyword and find the Xpath and perform click action but it should be for student only. – jainmiah Shaik Jun 02 '23 at 13:50
  • @jainmiahShaik I'll update my answer by that time check if the first 2 locators works for you as well. – undetected Selenium Jun 02 '23 at 13:53
  • @jainmiahShaik Check out the updated code in the third and forth option and let me know the status. – undetected Selenium Jun 02 '23 at 13:59
  • I used //*[text()='One stop solution for all your education needs']//following::a[text()='Know More'][1] but its giving error – jainmiah Shaik Jun 02 '23 at 14:17
  • @jainmiahShaik Checkout the answer update wrt the text _One stop solution for all your education needs_ – undetected Selenium Jun 02 '23 at 14:25
  • WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='One stop solution for all your education needs']//following::*[text()='Know More']"))).click() , to make this code generic I used above code but its giving me error SyntaxError: Failed to execute 'evaluate' on 'Document': – jainmiah Shaik Jun 02 '23 at 14:58
  • @jainmiahShaik Now you are making a simple usecae complicated :) Feel free to raise a new question with your new requirement. – undetected Selenium Jun 02 '23 at 20:49
0

Try this XPath:

//h5[text()='For Students']//following::a[text()='Know More'][1]
Shawn
  • 4,064
  • 2
  • 11
  • 23
  • instead of '//h5' can I give [text()='For Students']//following::a[text()='Know More'][1] – jainmiah Shaik Jun 02 '23 at 13:52
  • No, relative XPath always must start with `//`. If you do not want to give `h5`, then you can give `*` instead. Something like this `//*[text()='For Students']//following::a[text()='Know More'][1]` – Shawn Jun 02 '23 at 13:56
  • @Shawn I believe you still won't need that index i.e. `[1]` – undetected Selenium Jun 02 '23 at 14:01
  • I used //*[text()='One stop solution for all your education needs']//following::a[text()='Know More'][1] but its giving error. – jainmiah Shaik Jun 02 '23 at 14:12
  • @jainmiahShaik -What error does it give? if you inspect the DOM and copy-paste this XPath expression, does it locate 1 element? or zero elements? – Shawn Jun 02 '23 at 14:37
  • @undetectedSelenium - Hmm you may be right, however I was just ensuring that it locates the first occurrence of `Know More` node. Because there may be chances that there are more than one 'Know More' nodes and `following` will just locate/select all the nodes after the current node isn't it! – Shawn Jun 02 '23 at 14:42
  • can't we make it generic like this WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='One stop solution for all your education needs']//following::*[text()='Know More']"))).click() – jainmiah Shaik Jun 02 '23 at 14:59