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
  • 93
  • 9

2 Answers2

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 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
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
  • 22,146
  • 4
  • 14
  • 36