Python Selenium - Trying to find element by text on page

+1 vote
<h2 class="productTitle" id="productTitle923" onclick="goToProduct(0)">Beats Headphones</h2>

How can I search an entire page full of products like that and only click on the on that says "Beats Headphones" as an example. I've tried driver.find_element_by_xpath and a few other things I found online but nothing worked. Any ideas?

By the way sorry if this is an stupid question. I am completely new to this, any help would be appreciated. Thanks!
Dec 31, 2018 in Selenium by Sebastian
• 180 points
15,073 views
Hey @User! Which website are you using?

1 answer to this question.

+1 vote

Hello @User, Here is an example for Amazon.

I am opening Amazon.

I am searching for Boat Headphones

I am selecting a particular headphone.

Here is the code I used.

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("Boat Headphones",Keys.ENTER);
        //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();
    }
}

Let me know if this was what you were looking for.

Hope this will help!

To learn more, go for Python Master course today.

Thank!

answered Dec 31, 2018 by Priyaj
• 58,100 points
Not finding the element on the webpage

I apologize if this is my second time posting this question, but I don't think it posted the first time.

I have been trying this:

driver.find_element_by_link_text('Beats Headphones').click()

But get an error, which I will show below. From what I can understand it is not finding the element on the webpage even though I am copying and pasting the exact text from the webpage. I even tried 

driver.find_element_by_partial_link_text('Product Here')

with nothing being found. I'd link you the website but it requires a login. If theres a way to send that privately I would be more than happy to send it. Thank you again. Here is the error:

Traceback (most recent call last):

  File "bot.py", line 45, in <module>

    order()

  File "bot.py", line 24, in order

    driver.find_element_by_link_text('9.99').click()

  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in find_element_by_link_text

    return self.find_element(by=By.LINK_TEXT, value=link_text)

  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element

    'value': value})['value']

  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute

    self.error_handler.check_response(response)

  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response

    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"9.99"}

  (Session info: chrome=71.0.3578.98)

  (Driver info: chromedriver=2.45.615355 (d5698f682d8b2742017df6c81e0bd8e6a3063189),platform=Mac OS X 10.13.6 x86_64)
Hey @sebastian, can you tell me as which website are you working on?

I will take a look on your error and reply asap.

Hey @sebastian with what i understood about your question I can figured out this.

I searched boat headphones on google and clikced on a particular name(this name is known to me)

This is the code I used then

import java.awt.RenderingHints.Key;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GoogleProduct {
    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.google.com");
        driver.findElement(By.name("q")).sendKeys("Boat headphones",Keys.ENTER);
        driver.findElement(By.linkText("Boat Rockerz 400 On-Ear Bluetooth Headphones (Carbon Black)")).click();
    }
}

Please let me know is this what you were looking for.

@Priyaj

You will have to make a free account to get to the product page.
Hey @sebastian, not getting access to the website!

I think it could be because it is in multiple classes buried pretty deep but I'm not sure if that makes a difference.

So for example, if I wanted the 2019 Wall Calendar by Camoleaf I typed

driver.find_element_by_partial_link_text('2019 Wall Calendar by Camoleaf').click()

You can also see more here:

https://stackoverflow.com/questions/53991055/selenium-not-finding-element-by-link-text

@sebastian, the website that you gave me, I will try solving your question on that itself.
Hey @Sebastian, may i ask you what's your objective? Is it just to use link text locator or do you have a particular problem statement related to this issue?

Related Questions In Selenium

0 votes
1 answer

Python + Selenium - Trying to find element by link containing certain words

2 issues. One thing is, "Catalogues" & ...READ MORE

answered Mar 28, 2018 in Selenium by nsv999
• 5,500 points
9,307 views
0 votes
2 answers

Python & Selenium: How to find and click on YouTube's Like button

Hello Nitin, as the Like button on ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
5,387 views
0 votes
1 answer

How to find Elements by their Attribute in Python Selenium WebDriver

To find all the <td> tags which have attribute data-sortable-id you ...READ MORE

answered Jul 19, 2018 in Selenium by Samarpit
• 5,910 points
10,548 views
0 votes
1 answer
0 votes
1 answer

Installing Selenium Webdriver with Python package

Hey Hemant, for installing Selenium Webdriver with ...READ MORE

answered May 8, 2019 in Selenium by Anvi
• 14,150 points
15,772 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,568 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,084 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,178 views
0 votes
2 answers

Unable to Click on an Element in Selenium (Python) even after finding it.

Here, I give you working script which ...READ MORE

answered Sep 19, 2018 in Selenium by Priyaj
• 58,100 points
24,068 views
+1 vote
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP