简体   繁体   中英

Selenium + Python unable to locate element

Trying to get:

<a title="Size 11.0" value="11.0" data-modelsize="11_0" data-ssi="false" data-sfs="false" data-backorder="false" class="grid_size in-stock" href="javascript:void(0);">11.0</a>

this element did work at some point but now its returning this error:

Unable to locate element: /html/body/div[3]/div[1]/div[2]/section[2]/section/form/div[5]/span[1]/a[11]

It's not in a frame so I know that's not the problem just hit a brick wall too.

Sometimes it throws unable to locate element error and sometimes it will work. Very confusing.

Without the rest of your code, it's hard to tell. My guess would be that it's a timing issue. When you click the Size button, it takes a fraction of a second to open... my guess is that sometimes you were clicking before it was completely open/available.

When doing something like selecting a size that you will likely reuse a lot, I write functions to handle that so that it's reusable. I wrote selectSize() to take care of clicking the Size button, selecting a size, and waiting for the size area to close. I just tested the code below and it's working.

driver.navigate().to("https://www.footlocker.com/product/model:256325/sku:CQ2011/");
selectSize("11.0");

...and the helper function

public static void selectSize(String size)
{
    driver.findElement(By.id("pdp_size_select_mask")).click();
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[value='" + size + "']"))).click();
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("size_selection_container")));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM