0

<div>

  <div class="alk_dvImage"><a href="/products/"><img class="alk_prImg" src="https://a random photo" alt="a random product"></a>
  </div>
  <div class="product-score"></div>
  

  <a href="/products/" class="alk_prName alk_pr" title="Products Title">Strong Graphic Card
  </a>

</div>

Lets assume we have a html as given above. I want to extract the title of the 'a' element which is nested in a div. And also i want the class of this same element how ever when i try this code

browser.find_element_by_css_selector('a.alk_prName alk_pr')

this does not respond anything. Btw i couldnt do anything to get tite of a element.

2 Answers 2

1

To print the value of the title attribute i.e. Products Title you can use either of the following Locator Strategies:

  • Using css_selector:

    print(driver.find_element(By.CSS_SELECTOR, "a.alk_prName.alk_pr[href='/products/']").get_attribute("title"))
    
  • Using xpath:

    print(driver.find_element(By.XPATH, "//a[@class='alk_prName alk_pr' and @href='/products/'][contains(., 'Strong Graphic Card')]").get_attribute("title"))
    

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

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.alk_prName.alk_pr[href='/products/']"))).get_attribute("value"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='alk_prName alk_pr' and @href='/products/'][contains(., 'Strong Graphic Card')]"))).get_attribute("value"))
    
  • 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

How can i find more than one element by using (WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.alk_prName.alk_pr[href='/products/']"))).get_attribute("value") this method. I need every element in the page where classes equals a.alk_prName.alk_p. How ever this returns me only one element.
@omneer Of coarse this question title reads as title of a element, for more than one element feel free to raise a new ticket.
0

What happens?

Your not chaining the classes by dot in your selector, try the following:

browser.find_element_by_css_selector('a.alk_prName.alk_pr').get_attribute("title")

Example:

from selenium import webdriver
browser = webdriver.Chrome('C:\Program Files\ChromeDriver\chromedriver.exe')

html_content = """
  <a href="/products/" class="alk_prName alk_pr" title="Products Title">Strong Graphic Card</a>
"""

browser.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))

browser.find_element_by_css_selector('a.alk_prName.alk_pr').get_attribute("title")

1 Comment

Why did you use html_content = <a href="/products/" class="alk_prName alk_pr" title="Products Title">Strong Graphic Card</a> this is not whole html script. By the way i get the title thanks to you.

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.