1

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..

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • Maybe selenium has retrieved the element before it has had a chance to become active. Have you tried putting a slight pause just before retrieving the element after the click? Or you could even debug and step slowly – Paddyd Sep 29 '15 at 15:59
  • @Paddyd, Edited my question. – Koray Tugay Sep 29 '15 at 16:04
  • I would rather recommend to use expected conditions with webdriver wait – dmr Sep 29 '15 at 16:05
  • Long shot, but have u got javascript enabled for your browser? driver.setJavascriptEnabled(true); – Paddyd Sep 29 '15 at 16:11
  • @Paddyd When I enable js, I am in bigger trouble I get this: Exception in thread "main" org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: VBScript not supported in Window.execScript(). (http://scripts.hepsiburada.net/assets/sfstatic//Scripts.b.1.0.2199.0/build/shims.js#1) – Koray Tugay Sep 29 '15 at 16:13
  • Strange. I would suggest using a different webDriver (chrome or firefox for example) to see if the problem persists. I have no experience with the HtmlUnitDriver() – Paddyd Sep 29 '15 at 16:22

3 Answers3

1

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]-->
dmr
  • 526
  • 2
  • 8
  • When I open the page with a Browser (like Opera or Safari), the button is not disabled in my view. What do you mean it is disabled? – Koray Tugay Sep 29 '15 at 16:05
  • It has attribute disabled, perhaps it is an issue, if you want to work around this issue you can execute javascript which will remove this disabled attribute from this element. – dmr Sep 29 '15 at 16:07
  • What do you mean it is an issue? I know it has a disabled attribute but why? Why not when I visit with Opera? – Koray Tugay Sep 29 '15 at 16:10
  • bc there is an condition in markup that creates this button if it browser is IE9, I could assume that htmlunit without caps are treated like this, could you try to use it with caps https://code.google.com/p/selenium/wiki/HtmlUnitDriver – dmr Sep 29 '15 at 16:18
  • What is capabilities in here: HtmlUnitDriver driver = new HtmlUnitDriver(capabilities); ? – Koray Tugay Sep 29 '15 at 16:24
  • probably that would help http://stackoverflow.com/questions/7926246/why-doesnt-htmlunitdriver-execute-javascript – dmr Sep 29 '15 at 16:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90917/discussion-between-dmr-and-koray-tugay). – dmr Sep 29 '15 at 16:37
0

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();
JeffC
  • 22,180
  • 5
  • 32
  • 55
0

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();
    }

}
Anuj Kumar
  • 163
  • 10