java - 无法点击元素 - Selenium WebDriver

标签 java css selenium selenium-webdriver xpath

我正在尝试点击结帐按钮。这是它的样子:

enter image description here

和相同的 HTML 片段:

<div id="buy-button-next">
   <span data-reactroot="">
      <div data-cid="buy-button--enabled" style="align-items: center; border-style: solid; border-width: 0px; box-sizing: border-box; display: flex; flex-basis: auto; flex-direction: column; flex-shrink: 0; margin: 0px; padding: 0px; position: relative; min-height: 0px; min-width: 0px;">
         <div style="align-items: stretch; border-style: solid; border-width: 0px; box-sizing: border-box; display: flex; flex-basis: auto; flex-direction: column; flex-shrink: 0; margin: 0px; padding: 0px; position: relative; min-height: 0px; min-width: 340px;">
            <button data-cid="button.buy_button" style="padding: 0px; margin: 0px; background-color: rgba(255, 255, 255, 0); border: none; cursor: pointer; outline: 0px; -webkit-tap-highlight-color: rgba(255, 255, 255, 0);">
               <div style="align-items: stretch; border-style: solid; border-width: 1px; box-sizing: border-box; display: flex; flex-basis: auto; flex-direction: column; flex-shrink: 0; margin: 0px; padding: 4px; position: relative; min-height: 0px; min-width: 0px; background-color: rgb(52, 52, 52); border-color: rgba(255, 255, 255, 0); border-radius: 3px; height: 50px; transition: background-color 0.2s ease, border-color 0.2s ease;">
                  <div style="align-items: stretch; border-style: solid; border-width: 0px; box-sizing: border-box; display: flex; flex-basis: auto; flex-direction: column; flex-shrink: 0; margin: 0px; padding: 0px; position: absolute; min-height: 0px; min-width: 0px; background-color: rgba(255, 255, 255, 0); border-radius: 3px; bottom: -1px; left: -1px; opacity: 1; right: -1px; top: -1px; transition: background-color 0.2s ease, opacity 0.2s ease; z-index: 1;"></div>
                  <div style="align-items: center; border-style: solid; border-width: 0px; box-sizing: border-box; display: flex; flex-basis: auto; flex-direction: column; flex-shrink: 0; margin: 0px; padding: 0px 21px; position: relative; min-height: 0px; min-width: 0px; background-color: rgba(255, 255, 255, 0); border-radius: 3px; height: 100%; justify-content: center; transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease; z-index: 2;">
                     <div style="align-items: stretch; border-style: solid; border-width: 0px; box-sizing: border-box; display: flex; flex-basis: auto; flex-direction: column; flex-shrink: 0; margin: 0px; padding: 0px; position: absolute; min-height: 0px; min-width: 0px; align-self: center; top: 50%; transform: translateY(-2px);"></div>
                     <span style="max-width: 100%; color: rgb(255, 255, 255); font-family: &quot;Klarna Sans&quot;, Helvetica, Arial, sans-serif; font-weight: 700; font-size: 16px; opacity: 1; transition: color 0.2s ease; visibility: visible; -webkit-font-smoothing: antialiased; text-rendering: geometricPrecision; text-size-adjust: none;">Place Order</span>
                  </div>
               </div>
            </button>
         </div>
         <div style="align-items: stretch; border-style: solid; border-width: 0px; box-sizing: border-box; display: flex; flex-basis: auto; flex-direction: column; flex-shrink: 0; min-height: 0px; min-width: 0px; background-color: rgba(255, 255, 255, 0); width: 100%; height: 30px;"></div>
      </div>
   </span>
</div>

所有内容都加载到 iFrame 中,所以我也切换到 iFrame 中。

driver.switchTo().frame("klarna-checkout-iframe");

但是按钮有奇怪的 html。

我做过的代码试验:

css=button[data-cid='button.buy_button'] span

xpath=//div/span[contains(text(),'Place Order')]

我在等待此元素出现时收到超时异常。

Timed out after 35 seconds waiting for presence of element located by: By.cssSelector: button[data-cid='button.buy_button'] span

最佳答案

根据您共享的 HTML 和您的代码试验,首先您必须为 frameToBeAvailableAndSwitchToIt 引入 WebDriverwait,然后可能继续前进您将在 Checkout Button 上调用 click(),因此您需要诱导 WebDriverWait 以使所需的 元素可点击 并且您可以使用以下任一解决方案:

new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("klarna-checkout-iframe")));
//new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("klarna-checkout-iframe")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='buy-button-next']//button[@data-cid='button.buy_button']//span[contains(.,'Place Order')]"))).click();

关于java - 无法点击元素 - Selenium WebDriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51472320/

相关文章:

java - Java Instrumentation Agent 是否支持类中的新方法定义?

jquery - 如何让这个菜单延迟一秒钟的效果?

css - 可折叠下拉菜单 - 为处于事件状态的菜单设置背景?

java - servlet 不显示连接两个表的所有匹配结果

java - 在 Openshift 上的 servlet 中打开资源文件

html - Angular 。使子级 100% 宽度为 flex 父级

xpath - 获取网页所有可见元素

Python Selenium : Is there a way to install webdriver inside the virtual enviroment

java - 无法在 Eclipse Neon 上安装 TestNG。提示输入用户名和密码

java - 为什么所有 javascript 网格小部件都使用 div?