ehuff
ehuff

Reputation: 119

How to find a value inside of a class using xpath? (Selenium ChromeDriver)

I am trying to use ChromeDriver to extract the value "/mva/library/20120730/93135a040s.gif" from the following html code:

enter image description here

Currently, my code:

is_black_white = driver.find_elements_by_xpath("//div[@class='aj cw cy db ImgCaptionCntnrHover']/img[@data-filterwithidind='True']")

x = is_black_white[0].get_attribute("title src")

print(x)

is returning "None".

I feel like I'm so close. Any help would be very much appreciated!

Thanks!

edit: the full url is https://www.mcmaster.com/94735A701/

Upvotes: 0

Views: 265

Answers (1)

Sureshmani Kalirajan
Sureshmani Kalirajan

Reputation: 1938

The xpath is incorrect. You can try this solution. Please note there is a double slash which indicates any child node with img tag.

WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='aj cw cy db ImgCaptionCntnrHover']//img")))
 
is_black_white = driver.find_elements_by_xpath("//div[@class='aj cw cy db ImgCaptionCntnrHover']//img")

print(len(is_black_white))
x = is_black_white[0].get_attribute("src")
print(x)

#Prints all the src urls
for ele in is_black_white:
    print(ele.get_attribute("src"))

Output:

4
https://www.mcmaster.com/mva/library/20120730/93135a040s.gif
https://www.mcmaster.com/mva/library/20120730/93135a040s.gif
https://www.mcmaster.com/mva/library/20120730/94735a040s.gif
https://www.mcmaster.com/mva/library/20120730/93135a040s.gif
https://www.mcmaster.com/mva/library/20120730/94735a040s.gif

Upvotes: 1

Related Questions