Search code examples
pythonseleniumxpathwebdriverwaitlinktext

How to click <a class href > link question using Selenium Python


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.


Solution

  • 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