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

Cannot make a simple find element with a cssSelector using Selenium WebDriver

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

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

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

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

试试这个:

driver.findElement(By.cssSelector("h4[class='code']")).click();

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