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.

omneer
  • 79
  • 9

2 Answers2

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")
HedgeHog
  • 12,487
  • 2
  • 11
  • 31
  • Why did you use `html_content = Strong Graphic Card` this is not whole html script. By the way i get the title thanks to you. – omneer Jan 15 '21 at 20:24
  • @omneer Just focused on your question / issue - [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) It also works with the whole html, but this was not needed showing whats wrong and what works. But anyway - Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted - [someone-answers](https://stackoverflow.com/help/someone-answers) - Thanks – HedgeHog Jan 15 '21 at 20:42
0

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
    
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
  • 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 Jan 16 '21 at 12:14
  • @omneer Of coarse this question title reads as _title of a element_, for more than one element feel free to raise a new ticket. – undetected Selenium Jan 16 '21 at 17:17