javascript html元素无法通过Java Selenium“jsExecutor”点击

pb3s4cty  于 2023-06-20  发布在  Java
关注(0)|答案(2)|浏览(105)

我有以下JS HTML元素:

document.querySelector('[value="fixedsumcredit_kp"]')
<input id=​"-0198e3aa-d2b1-46ce-bdf0-192e60662cd3-fixedsumcredit_kp" type=​"radio" name=​"-0198e3aa-d2b1-46ce-bdf0-192e60662cd3" value=​"fixedsumcredit_kp" style=​"height:​ 100%;​ opacity:​ 0;​ position:​ absolute;​ width:​ 100%;​ top:​ 0px;​ left:​ 0px;​ z-index:​ 1;​ cursor:​ pointer;​ -webkit-tap-highlight-color:​ rgba(255, 255, 255, 0)​;​">​

我尝试通过Selenium和jsExecutor点击它,如下所示:

@FindBy(xpath = "//input[@value='fixedsumcredit_kp']")
    public WebElement oneKlarna_ratenkauf_6;

    jsExecutor.executeScript("arguments[0].click();", oneKlarna_ratenkauf_6);

和/或

WebElement oneKlarna = (WebElement) jsExecutor.executeScript("document.querySelector(\"value='fixedsumcredit_kp'\")");

        System.out.println("oneKlarna: " + oneKlarna);

        jsExecutor.executeScript("arguments[0].click();", oneKlarna);

第一个脚本不点击,第二个脚本不创建JS“WebElement oneKlarna”控制台点击另一方面在Chrome开发者控制台工作正常:

document.querySelector('[value="fixedsumcredit_kp"]').click()

我的问题是:如何正确点击Selenium中显示的“input”HTML元素?
更新:
我在HTML元素周围有以下iframe

<iframe id="klarna-klarna-main" name="klarna-klarna-main" title="Klarna" scrolling="no" frameborder="0" src="https://js.playground.klarna.com/eu/kp/v1.0.0-23083-g146e667ef2/main.html#data=%7B%22onShowExternalDocumentRegistered%22%3Afalse%2C%22fullscreenIframeID%22%3A%22klarna-klarna-fullscreen%22%2C%22popupWindowEnabled%22%3Afalse%2C%22nativeHookApiSupported%22%3Afalse%2C%22nativeHookApiFeatures%22%3A%5B%5D%2C%22paymentMethodCategory%22%3A%22klarna%22%2C%22purchaseCountry%22%3A%22DE%22%2C%22scheme%22%3Atrue%2C%22sessionType%22%3A%22payments%22%2C%22sessionID%22%3A%2270b4ebfe-6ae5-5596-8a24-8ddcd540e0d3%22%2C%22merchantName%22%3A%22Playground%20Demo%20Merchant%22%2C%22environment%22%3A%22playground%22%7D&amp;" style="height: 701px; width: 100%; max-width: 600px; min-width: 280px; display: inline;"></iframe>

你知道这种行为吗?

c8ib6hqw

c8ib6hqw1#

该元素是一个动态元素,因此要单击该元素,您需要为elementToBeClickable()诱导WebDriverWait,您可以使用以下locator strategies之一:

  • 使用 cssSelector
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[id$='fixedsumcredit_kp'][value='fixedsumcredit_kp']"))).click();
  • 使用 xpath
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(@id, 'fixedsumcredit_kp') and @value='fixedsumcredit_kp']"))).click();
osh3o9ms

osh3o9ms2#

Driver.getDriver().switchTo().defaultContent(); // you are now outside both frames
        Driver.getDriver().switchTo().frame("klarna-klarna-main");

成功了!

相关问题