java - 使用 Selenium 和 Java 13 自动化 Span

标签 java selenium xpath css-selectors java-13

我正在使用 Java 中的 selenium 实现网站自动化。

<a id="pd-vote-button10359300" class="css-vote-button pds-vote-button"><span>Vote</span></a>

对于这个按钮,我需要在 Selenium 中自动单击。我正在遵循但不工作。

WebElement click = driver.findElement(By.id("pd-vote-button10359300"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", click);

您能否提出问题是什么?

之后

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))).click();

我收到以下错误

Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <span>...</span> is not clickable at point (122, 877). Other element would receive the click: <div class="nc_wrapper swp_floating_horizontal_wrapper bottom" style="background-color: rgb(255, 255, 255);">...</div>
 (Session info: chrome=77.0.3865.90)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'Sanjeevans-iMac.local', ip: '169.254.10.5', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.6', java.version: '13'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.90, chrome: {chromedriverVersion: 76.0.3809.126 (d80a294506b4..., userDataDir: /var/folders/k2/8cltlrwj23n...}, goog:chromeOptions: {debuggerAddress: localhost:59693}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 02cb57fb86be956e5e10be634b5724b1

    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)

    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)

    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)

    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)

    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)

    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)

    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)

    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)

    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)

    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)

    at bigboss.A.main(A.java:49)

最佳答案

要在元素上click(),您必须为elementToBeClickable()引发WebDriverWait,并且您可以使用以下任一Locator Strategies :

  • css选择器:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))).click();
    
<小时/>

更新

作为替代方案,您可以使用 executeScript() 方法,如下所示:

  • css选择器:

    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))));
    
  • xpath:

    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))));
    

注意:当您使用 java.version: '13' 时,值得一提的是 Selenium/ 之间存在一些兼容性问题,您可以详细讨论见:

<小时/>

tl;博士

关于java - 使用 Selenium 和 Java 13 自动化 Span,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58061524/

相关文章:

列中带有 ImageView 数组的 JavaFX TableView

python - 如何使用Python在Selenium WebDriver中获取用户代理信息

python - 如何使用 Python (Selenium) 查找下拉菜单中值的数量

java - JDOM XPath 获取没有命名空间的内部元素

java - Android Firebase 保持 addListenerForSingleValueEvent 更新

java - 最好使用什么数据结构? java

xpath - 排除某些特定元素的xpath

Python 3.6 Selenium 如何通过父节点的引用找到子节点(xPath)

java - 分割二维数组(JAVA)

java - Allure Cucumber JVM 适配器 - 报告显示没有功能没有故事