2

Hi I am new to Selenium Python Webdriver. Trying to click the link Abc by using find_element_by_link_text which didn't work ,

<a class="favorite-link " href="https://onesource.passporthealth.com/_members/query/osusf/Default.aspx?dlpvid=0000&amp;sid=-111111111">Abc</a>

I will appreciate the help. Any idea how to use xpath for this case?

          <div id="favCol1" class="col-md-6 col-sm-6" style="padding: 2px; margin: 0px;">
                                    <div id="favId2861214" class="elig-item">
                                        <div title="Remove payer from favorites list" class="delete-favorite" data-itemname="Aetna" data-favid="2861214" data-dsid="28" data-lobid="1"></div>
                                        <a class="favorite-link " href="https://onesource.passporthealth.com/_members/query/osusf/Default.aspx?dlpvid=1111&amp;sid=-000000">Abc</a>
                                        <span class="side-text"></span>
                                    </div>

                                    
                                    <div id="favId2869169" class="elig-item">
                                        <div title="Remove payer from favorites list" class="delete-favorite" data-itemname="CIGNA" data-favid="2869169" data-dsid="317" data-lobid="1"></div>
                                        <a class="favorite-link " href="https://onesource.passporthealth.com/_members/query/osusf/Default.aspx?dlpvid=0000&amp;sid=-11111133323">efg</a>
                                        <span class="side-text"></span>
                                    </div>

                                    
                                    <div id="favId2861157" class="elig-item">
                                        <div title="Remove payer from favorites list" class="delete-favorite" data-itemname="Florida Blue" data-favid="2861157" data-dsid="35" data-lobid="1"></div>
                                        <a class="favorite-link " href="https://onesource.passporthealth.com/_members/query/osusf/Default.aspx?dlpvid=2323&amp;sid=-1111111">xyz</a>
                                        <span class="side-text"></span>
                                    </div>

                                    
                                    <div id="favId2861963" class="elig-item">
                                        <div title="Remove payer from favorites list" class="delete-favorite" data-itemname="Humana" data-favid="2861963" data-dsid="82" data-lobid="1"></div>
                                        <a class="favorite-link " href="https://onesource.passporthealth.com/_members/query/osusf/Default.aspx?dlpvid=1233&amp;sid=-000021212">HIJ</a>
                                        <span class="side-text"></span>
                                    </div>                             
                                </div>

Tried:

.find_element_by_xpath("//*[contains(@class, 'favorite-link') and contains(text(), 'Abc')]/a").click()

update: The whole block was embedded in an iFrame. That's why I couldn't access. Had to switch to that frame.

1
  • Any particular problem with it? Commented Feb 11, 2021 at 5:48

2 Answers 2

2

To click on the element with text as Abc you can use either of the following Locator Strategies:

  • Using LINK_TEXT:

    driver.find_element(By.LINK_TEXT, "Abc")
    
  • Using XPATH:

    driver.find_element(By.XPATH, "//div[@class='elig-item']//a[@class='favorite-link ' and text()='Abc']").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Abc"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='elig-item']//a[@class='favorite-link ' and text()='Abc']"))).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
    
Sign up to request clarification or add additional context in comments.

2 Comments

It still can't find the element. Also tried the Webdriverwait. I keep getting Timeout. Increased the time. No success yet! This is weird, should have worked by now. Thanks though.
I think the whole block is embedded in an iFrame. That's why I couldn't access
2
 driver.find_element_by_link_text("Abc").click()

Just click the tag with the link text Abc.

 driver.find_element_by_xpath("//*[text()='Abc']").click()

5 Comments

I tried that. But does not work. I guess because there is also class="favorite-link " . Probably i will have to use xpath. But don't know how to use it.
You can just grab the xpath from the developer console by inspecting it and right click copy xpath.
You can just need to add div[@id='']/ to the above in the xpath for it to work.
I have also tried that the xpath i copied was browser.find_element_by_xpath('//*[@id="favId2861963"]/a').click() , but didn't work. says element wasn't found. Thanks though!
That block is embedded in an iFrame. That's why I couldn't access. Had to switch to that frame.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.