【问题标题】:Selenium shows me a button as disabled, why?Selenium 向我显示一个禁用的按钮,为什么?
【发布时间】:2015-12-27 04:52:37
【问题描述】:

我有这个示例代码:

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

当我运行这段代码时,元素将被打印为:

<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}">

我不明白为什么这个按钮被禁用了?当我使用浏览器导航到同一页面时,该按钮未被禁用。

示例页面:http://www.hepsiburada.com/htc-one-m8-p-TELCEPHTCM8-G

编辑

我也试过了:

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

这不起作用...我超时了..

【问题讨论】:

  • 也许 selenium 在它有机会激活之前检索了元素。您是否尝试在单击后检索元素之前稍作停顿?或者你甚至可以调试并慢慢进行
  • @Paddyd,编辑了我的问题。
  • 我宁愿推荐使用 webdriver wait 的预期条件
  • 远射,但你的浏览器启用了 javascript 吗? driver.setJavascriptEnabled(true);
  • @Paddyd 当我启用 js 时,我遇到了更大的麻烦我得到了这个:线程“main”中的异常 org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: VBScript not supported in Window.execScript()。 (scripts.hepsiburada.net/assets/sfstatic//Scripts.b.1.0.2199.0/…)

标签: java selenium


【解决方案1】:

Button 被禁用是因为它具有属性 disabled="disabled",它是这样显示的,因为它是这样实现的,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]-->

【讨论】:

  • 当我使用浏览器(如 Opera 或 Safari)打开页面时,该按钮在我的视图中未被禁用。你是什​​么意思它被禁用了?
  • 它的属性被禁用了,也许这是个问题,如果你想解决这个问题,你可以执行 javascript 来从这个元素中删除这个禁用的属性。
  • 这是什么问题?我知道它有一个 disabled 属性,但为什么呢?当我访问 Opera 时,为什么不呢?
  • bc 如果浏览器是 IE9,标记中有一个条件会创建此按钮,我可以假设没有大写的 htmlunit 会这样处理,您可以尝试将其与大写一起使用吗code.google.com/p/selenium/wiki/HtmlUnitDriver跨度>
  • 这里的功能是什么:HtmlUnitDriver driver = new HtmlUnitDriver(capabilities); ?
【解决方案2】:

也许它与 HTMLUnitDriver 有关?下面的代码对我来说很好。

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

【讨论】:

    【解决方案3】:

    在 html 源代码中,如果您看到按钮具有 IE9 的 disabled="disabled" 属性。 HtmlUnitDriver 可能把它当作 IE9,所以你可以尝试将 BrowserVersion 更改为 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();
        }
    
    }
    

    【讨论】:

    • 输出: &lt;button type="button" class="button big with-icon" id="addToCart" data-catalogname="Kuyumcu" data-isvariants="false" data-bind="click: $parent.addCurrentItemToCart.bind($parent), attr:{'data-price':webtrekkCost, 'data-sku':sku, 'data-loginstatus':webtrekkLoginStatus}, css:{'pre': $parent.isPreOrder()}" data-sku="TKNMRB001"&gt;
    猜你喜欢
    • 1970-01-01
    • 2012-02-27
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多