Fariba
Fariba

Reputation: 23

Selenium WebDriver java

I am using Mozilla firefox 44.0.1 & java version 1.8 & selenium Version 2.48

I am using following Web Page

I need to select an item & press "I want to buy this item" button, which creates 1 item in basket area. as soon as you add, basket on top right gets updated. you will see:

1 item/s View Basket

I am having problem locating this item , & I get "unable to locate element" error. I have following line to locate the element:

String count = driver.findElement(By.id("buyBasketCount")).getText();

and I tried:

String count = driver.findElement(By.xpath("//div[@class='buyBasketContent']/td[1]/span")).getText();

How do I access this element? I am trying to get value of one returned by variable count. but I get unable to locate element.

How do I access this element in the page?

Upvotes: 0

Views: 109

Answers (3)

Prateek
Prateek

Reputation: 1145

You can use Thread.sleep(enter some time in milliseconds) but that is never a recommended way.

Rather use this:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("buyBasketCount")));
String count = driver.findElement(By.id("buyBasketCount")).getText(); 

Upvotes: 2

Sarhanis
Sarhanis

Reputation: 1587

Should use FluentWait. Something like the following:

FluentWait<WebDriver> ajaxWait = new FluentWait<>(driver)
            .withTimeout(15, TimeUnit.SECONDS)
            .pollingEvery(75, TimeUnit.MILLISECONDS)
            .ignoring(AssertionError.class);
final By by = By.id("buyBasketCount");
ajaxWait.until(new Predicate<WebDriver>() {
    public boolean apply(final WebDriver input) {
        return driver.findElements(by).size() > 0;
    }
});
String count =  driver.findElement(by).getText();

Upvotes: 1

Alaa Abuzaghleh
Alaa Abuzaghleh

Reputation: 1009

try

Thread.sleep(1000); 

before

String count = driver.findElement(By.id("buyBasketCount")).getText(); 

Or

String count = driver.findElement(By.xpath("//div[@class='buyBasketContent']/td[1]/span")).getText();

Upvotes: -2

Related Questions