Reputation: 97
I have the following element. I tried css / xpath / class to locate the element but it failed. I think the reason of the failure is because another img at the bottom of the pic shares the same class and attribute name. I used find_elements as well but it could not detect the element either.
I even used absolute path but this could not get_attribute('src')
'/html/body/div[1]/div[2]/div/main/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div/ul/li[2]/button/picture/img'
For reference, the full xpath for the second img:
/html/body/div[1]/div[2]/div/main/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div/ul/li[3]/button/picture/img
I'm trying to save a product img from the following product page. https://www.sephora.com/product/squalane-vitamin-c-rose-oil-P416563?icid2=skugrid:p416563
Upvotes: 1
Views: 91
Reputation: 33361
You can locate this element according to it's src
attribute as following:
//img[contains(@src,'sku/s2382166-main-zoom')]
i.e.
driver.find_element_by_xpath("//img[contains(@src,'sku/s2382166-main-zoom')]")
UPD
This should work for all the similar pages on that web site
driver.find_element_by_xpath("//div[@data-comp='Carousel ']//img[contains(@src,'-main-zoom')]")
Upvotes: 0
Reputation: 1352
You can try the below XPath based on the index for the multiple tag with same name and attribute, Just change the index of it [1][2]
((.//button[@class='css-1flj7f7']//img)[1])
Upvotes: 0
Reputation: 29362
You can do xpath indexing for same tag
and same attributes
:-
//img[contains(@class, 'css')]
let's say above xpath represent more than one web element in DOM.
You can locate the first element like this :
(//img[contains(@class, 'css')])[1]
second webelement like this :
(//img[contains(@class, 'css')])[2]
and so on..
or you can use find_elements
as well :
all_names = driver.find_elements(By.XPATH, "//img[contains(@class, 'css')]")
all_names[0].click() #to click on first element
all_names[1].click() #to click on seconf element
Upvotes: 1