Koray Tugay
Koray Tugay

Reputation: 23800

Selenium shows me a button as disabled, why?

I have this sample code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class App {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new HtmlUnitDriver();
        driver.get("http://www.hepsiburada.com");
        WebElement element = driver.findElement(By.xpath("//*[@id=\"tabBestSelling\"]/div/div/div/div/div/ul/li[1]/div/a"));
        element.click();
        System.out.println("Page title is: " + driver.getTitle());
        element = driver.findElement(By.xpath("//*[@id=\"addToCart\"]"));
        System.out.println(element);
        driver.quit();
    }
}

When I run this code, element will be printed as:

<button type="button" class="btn m button" id="addToCart" data-catalogname="Telefon" data-isvariants="true" disabled="disabled" data-bind="click: $parent.addCurrentItemToCart.bind($parent), attr:{'data-price':webtrekkCost, 'data-sku':sku, 'data-loginstatus':webtrekkLoginStatus}">

I do not understand why this button is disabled? When I navigate to the same page with my browser, the button is not disabled.

An example page: http://www.hepsiburada.com/htc-one-m8-p-TELCEPHTCM8-G

Edit

I also tried:

    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"addToCart\"]")));

which did not work... I get a timeout..

Upvotes: 1

Views: 1976

Answers (3)

Anuj Kumar
Anuj Kumar

Reputation: 163

In the html source code if you see button has attribute disabled="disabled" for IE9. HtmlUnitDriver might be taking it as IE9, so you can try to change BrowserVersion to FIREFOX_38

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import com.gargoylesoftware.htmlunit.BrowserVersion;

public class Issue5 {

    public static void main(String[] args) throws InterruptedException {

        WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38, true);

        driver.get("http://www.hepsiburada.com");
        WebElement element = driver
                .findElement(By.xpath("//*[@id=\"tabBestSelling\"]/div/div/div/div/div/ul/li[1]/div/a"));
        element.click();
        System.out.println("Page title is: " + driver.getTitle());
        element = driver.findElement(By.xpath("//*[@id=\"addToCart\"]"));
        System.out.println(element);
        element.click();
        driver.quit();
    }

}

Upvotes: 0

JeffC
JeffC

Reputation: 25596

Maybe it has something to do with HTMLUnitDriver? The code below works fine for me.

WebDriver driver = new FirefoxDriver();
driver.get("http://www.hepsiburada.com/");
driver.findElement(By.cssSelector("#tabBestSelling a")).click();
System.out.println("Page title is: " + driver.getTitle());
driver.findElement(By.id("addToCart")).click();

Upvotes: 0

dmr
dmr

Reputation: 556

Button is disabled because it has attribute disabled="disabled", it is shown that way because it is implemented that way, f.e.

  <!--[if lte IE 9]>
       <button type="button" class="btn m button" id="addToCart"
            data-catalogname="Telefon"
            data-isvariants="true" disabled="disabled"
            data-bind="click: $parent.addCurrentItemToCart.bind($parent), attr:{'data-price':webtrekkCost, 'data-sku':sku, 'data-loginstatus':webtrekkLoginStatus}">
       </button>
  <![endif]-->

Upvotes: 1

Related Questions