Selenium Node JS, can't click on specific element with xpath

Viewed 187

I am trying to make a free open source script to buy shoes, exactly in this page: https://www.zalando.es/nike-sportswear-nike-waffle-one-zapatillas-summit-whitewhite-black-orange-ni112o0jj-a11.html .

I can get elements and click them as usual with driver module

driver.findElement(By.id('uc-btn-accept-banner')).click();

Error happens when I try to pick a foot size, first opening the size selector with

driver.findElement(By.id('picker-trigger')).click();  //THIS ONE WORKS.
driver.findElement(By.xpath('/html/body/div[7]/div/div[3]/div/form/div/div[1]/div/label/span')).click();   // THIS ONE DOESN'T.

I tried clicking some elements by xpath on the site and all of them are working but the foot size. Can someone help me picking the foot size? I don't mind if it uses other method.

2 Answers

You are using a wrong selector there.
After opening the drop down menu you can select any size by locator like this:

//input[@name="size-picker"]/..//span[text()="39"]

It will be like this:

driver.findElement(By.id('picker-trigger')).click();
driver.findElement(By.xpath('//input[@name="size-picker"]/..//span[text()="39"]')).click();

Just update the value of desired size in the XPath expression

It it a drop down: (But we can not use Select class from Selenium since the drop down is not built using Select option tag)

Click on a drop down using this xpath :

//span[text()='Elige una talla']/..

and click like this :

driver.findElement(By.xpath("//span[text()='Elige una talla']/..")).click();  

Now you would see a list of foot size :

Select them with the below xpath :

//form[@name='size-picker-form']/descendant::label[contains(@for, 'size-picker')][3]

This will select 3rd item from the shown list. You have to change [3] to other number incase you would like to select different size.

driver.findElement(By.xpath("//form[@name='size-picker-form']/descendant::label[contains(@for, 'size-picker')][3]")).click(); 

This should select 40 size

Related