【发布时间】: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/…)