Home > Back-end >  Trying to get an element of a class using Selenium Python
Trying to get an element of a class using Selenium Python

Time:12-13

I just start learning python. So on this chapter it's basically to a create a price tracker using selenium.

The photo below is what I'm trying to get using selenium.

Screenshot

if i try

search_box = driver.find_element(By.CLASS_NAME, "a-price a-text-price a-size-medium apexPriceToPay")

it shows red sea of lines , saying no buch Message: no such element

Then I try

search_box = driver.find_element(By.CLASS_NAME, "a-offscreen")

the output is: <selenium.webdriver.remote.webelement.WebElement (session="0e1349ab77fff9cc0f9c565cc173927d", element="bbd837af-3cdf-4bd1-825c-9fd8cd27f719")>

I'm really new to programming...so I tried using .text at the end, no luck!

what can i do to get the price?

CodePudding user response:

I would try this:

search_box = driver.find_element(By.CLASS_NAME, "apexPriceToPay")

Or maybe

search_box = driver.find_element(By.CSS_SELECTOR, ".a-price.a-text-price")

To give a precise answer we need to see the entire page HTML to find the shortest unique locator

CodePudding user response:

You need to take care of a couple of things:

  1. You can't pass multiple classnames within driver.find_element(By.CLASS_NAME, "classA classB classC classD") as it may raise invalid selector.

  2. Passing the classname a-offscreen selects 18 elements, so you even can't use it.

  3. You will see the output as:

    <selenium.webdriver.remote.webelement.WebElement (session="0e1349ab77fff9cc0f9c565cc173927d", element="bbd837af-3cdf-4bd1-825c-9fd8cd27f719")>
    

    when you will print the element itself, which you don't need either.


Solution

To print the text $20.97 you can use the following Locator Strategy:

  • Using xpath and text attribute:

    print(driver.find_element(By.XPATH, "//td[starts-with(., 'Price')]//following-sibling::td[1]/span[contains(@class, 'a-price')]/span[@aria-hidden='true']").text)
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategy:

  • Using xpath, the second <span> and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[starts-with(., 'Price')]//following-sibling::td[1]/span[contains(@class, 'a-price')]/span[@aria-hidden='true']"))).get_attribute("innerHTML"))
    
  • Console Output:

    $20.97
    
  • 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
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


References

Link to useful documentation:

  • Related