0

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

  • You could try to find the sizes and colours with Regex, if you can find some pattern that is shared among the 'size' buttons and 'color' buttons. – DSteman Jun 17 '21 at 12:00

2 Answers2

1

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
Carlo 1585
  • 1,455
  • 1
  • 22
  • 40
0

The color buttons in the webpage seem to have a class named 'sku-property-image'. The sizes have 'sku-property-text'. Try to find_elements_by_class_name (example: Selenium Finding elements by class name in python). Then read what's inside of the element and click() conditionally.

DSteman
  • 1,388
  • 2
  • 12
  • 25
  • yes, but if I do this for size, i.e clicked at 1st size. then what I will do for colors? because both have second class with the same name and indexing – Abu Bakker Hashmi Jun 17 '21 at 12:34