website automation with selenium along with python

Issue

Hy there! I am automating aliexpress with selenium and python where users can buy products at voice command and can purchase any type of product. now the problem is a color and size selection, I have tried x-path but every element have a different x-path for the same color and size, I want a selector for at least four colors and four sizes, for clearance I have given the image, code, and link to the page too. if anyone has the solution plz mention it. thanx in advance

code :

#for selecting color 2 of an third item, but different for every element

elif '2' in query:                                 
                try:                                            
                    color_picker2=driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div/div[2]/div[7]/div/div[1]/ul/li[2]/div')
                    color_picker2.click()
                except:                                        
                    color_picker2=driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div/div[2]/div[6]/div/div/ul/li[2]/div')
                    color_picker2.click()  

link to the page is :
https://www.aliexpress.com/item/1005001621523593.html?spm=a2g0o.productlist.0.0.45157741uKKhLZ&algo_pvid=bd6c858e-759b-4c66-a59b-2b1724286123&algo_exp_id=bd6c858e-759b-4c66-a59b-2b1724286123-0

image (marked)for required details is :attached image for clearence

Solution

to select the type you can use the css selector and you can use this selector changing the index based on what you want select; I’m selecting the image but I think with only the div class sku-property-image should be enough:

First Model CSS Selector:

ul[class='sku-property-list']  li:nth-child(1) div[class='sku-property-image'] img

If you want select the second one just change 1 for 2:

ul[class='sku-property-list']  li:nth-child(2) div[class='sku-property-image'] img

For the Size the question is a bit more complex because size and country have the same selector so in this case you have to get the father element and hardcode the child of what you are looking for, as u can see in the below selector the div:nth-child(2) indicate the size section, instead li:nth-child(1) which size select, 1=S, 2=M, etc… example:

First SIZE S CSS Selector:

div[class='sku-wrap'] div:nth-child(2) ul[class='sku-property-list'] li:nth-child(1) div[class='sku-property-text'] span

Second SIZE M CSS Selector:

div[class='sku-wrap'] div:nth-child(2) ul[class='sku-property-list'] li:nth-child(2) div[class='sku-property-text'] span

Answered By – Carlo 1585

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published