Anon0441
Anon0441

Reputation: 67

Selenium not finding element by link text

I try using driver.find_element_by_partial_link_text('2019') but I get an error saying it was unable to find the element. I also tried using find_element_by_link_text('') and using the whole line but it wont work. Ideas?

driver.find_element_by_partial_link_text('2019').click()

That is what I have been trying with nothing working. Here is the webpage HTML:

<div class="rowOf" id="tableRow1">
    <div class="tableD">
        <div class="productDiv" id="productDiv92195">
            <h2 class="productTitle" id="productTitle92195" onclick="goToProduct(0)">2019 Wall Calendar by Camoleaf</h2>
            <img class="productImage" src="https://images-na.ssl-images-amazon.com/images/I/91j3pmPYDOL.jpg" onclick="goToProduct(0)">
            <hr>
            <h4 class="normalPrice" id="normalPrice0" onclick="goToProduct(0)">
                Normally: <span class="currency">$  </span>16.95
            </h4>
            <h4 class="promoPrice" style="margin:2.5px auto;" id="promoPrice92195" onclick="goToProduct(0)">
                Your Amazon Price: <span class="currency">$  </span>1.70
            </h4>
            <h3>Your Total: <span class="currency">$  </span>1.70</h3>
            <p class="clickToViewP" id="cToVP92195" onclick="goToProduct(0)">Click to view and purchase!</p>
        </div>
    </div>
    <div class="tableD">
        <div class="productDiv" id="productDiv69354">
            <h2 class="productTitle" id="productTitle69354" onclick="goToProduct(1)">Pure Lyft Energy Drink Mix (4 Pack) by PURELYFT</h2>
            <img class="productImage" src="https://images-na.ssl-images-amazon.com/images/I/81kCgs96Z0L.jpg" onclick="goToProduct(1)">
            <hr>
            <h4 class="normalPrice" id="normalPrice1" onclick="goToProduct(1)">
                Normally: <span class="currency">$  </span>9.99
            </h4>
            <h4 class="promoPrice" style="margin:2.5px auto;" id="promoPrice69354" onclick="goToProduct(1)">
                Your Amazon Price: <span class="currency">$  </span>0.99
            </h4>
            <h3>Your Total: <span class="currency">$  </span>0.99</h3>
            <p class="clickToViewP" id="cToVP69354" onclick="goToProduct(1)">Click to view and purchase!</p>
        </div>
    </div>
    <div class="tableD">
        <div class="productDiv" id="productDiv79478">
            <h2 class="productTitle" id="productTitle79478" onclick="goToProduct(2)">Multi-Purpose Calf Compression Sleeves by DS Sports</h2>
            <img class="productImage" src="https://images-na.ssl-images-amazon.com/images/I/91U7ExY-SfL.jpg" onclick="goToProduct(2)">
            <hr>
            <h4 class="normalPrice" id="normalPrice2" onclick="goToProduct(2)">
                Normally: <span class="currency">$  </span>12.95
            </h4>
            <h4 class="promoPrice" style="margin:2.5px auto;" id="promoPrice79478" onclick="goToProduct(2)">
                Your Amazon Price: <span class="currency">$  </span>5.05
            </h4>
            <h3>Your Total: <span class="currency">$  </span>5.05</h3>
            <p class="clickToViewP" id="cToVP79478" onclick="goToProduct(2)">Click to view and purchase!</p>
        </div>
    </div>
</div>

Upvotes: 4

Views: 2916

Answers (2)

Priyaj
Priyaj

Reputation: 61

Instead of using driver.findElement(By.partialLinkText("2019")); You should use driver.findElement(By.linkText("2019")); This again won't work as there are many calander in which the link text is 2019. So you need to provide a particular name. Example, I did this :-

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AmazonShoping {

    public static void main(String[] args) 
    {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\priyj_kumar\\Downloads\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.amazon.in");
        driver.findElement(By.id("twotabsearchtextbox")).sendKeys("2019 Calander",Keys.ENTER);
        driver.findElement(By.linkText("mapyourmonth Planner Organizer Diary Wall Calendar 2019")).click();
        //checking for a particular boat headphone say Boat BassHeads 900 Wired Headphone with Mic
//      driver.findElement(By.linkText("Boat BassHeads 900 Wired Headphone with Mic")).click();
//      String str = driver.findElement(By.xpath("//*[@id='mp-tfa']/p")).getText();
//      System.out.println(str);
    }

}

This worked fine for me. Please let me know if I misunderstood the question somehow.

Upvotes: 0

s3cur3
s3cur3

Reputation: 3025

In your sample HTML, the only instance of "2019" is in an <h2> tag, not an anchor (<a>) link. Since find_element_by_partial_link_text() only searches anchor tags, it won't find it.

You can search via XPath to find an arbitrary element via partial text. Something like this:

all_matches = driver.find_elements_by_xpath("//*[text()[contains(., '2019')]]")
all_matches[0].click()

That XPath says:

  1. Search all elements (*)
  2. Look at each item's text() in turn
  3. If that text() contains() the string "2019", add it to the set of matches.

And of course we only click on the first element that matches.

Upvotes: 4

Related Questions