无法使用Selenium WebDriver使用cssSelector创建简单的find元素

时间:2015-01-28 18:53:37

标签: selenium selenium-webdriver webdriver selenium-chromedriver

我一直试图以各种方式点击按钮而没有成功。每次我尝试,我总是得到这个堆栈错误

Driver info: driver.version: EventFiringWebDriver
    at sun.reflect.GeneratedConstructorAccessor21.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:441)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:426)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.openqa.selenium.support.events.EventFiringWebDriver$2.invoke(EventFiringWebDriver.java:101)
    at $Proxy1.findElement(Unknown Source)
    at org.openqa.selenium.support.events.EventFiringWebDriver.findElement(EventFiringWebDriver.java:184)
    at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:47)
    at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:1)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:168)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我要点击的按钮有这个HTML

<div id="" class="tile bg-blue shipping" style="" shippingid="100">
    <div class="tile-body text-center">
        <h4 class="code">ShippingOrder</h4>
    </div>
</div>

这是我用过的一些命令:

driver.findElement(By.cssSelector("h4[class='code'][value='ShippingOrder']")).click();
driver.findElement(By.xpath("//h4[contains(text(), 'ShippingOrder')]");
driver.findElement(By.cssSelector(".bg-blue.shipping[shippingid=\"100\"]")).click();

3 个答案:

答案 0 :(得分:0)

根据HTML代码段,没有元素,其中包含类code和属性value,其值为ShippingOrderShippingOrder是该元素的文本内容。 首先尝试这个:

print driver.findElement(By.cssSelector(".code")).getText();

答案 1 :(得分:0)

试试这个:

driver.findElement(By.cssSelector(&#34; H4 [类=&#39;代码&#39;]&#34))。单击();

答案 2 :(得分:-1)

问题是javascript构建按钮所花费的时间,所以在我搜索时html仍然不存在。 谢谢大家的帮助。