gpt4 book ai didi

Selenium:该元素无法滚动到 View 中

转载 作者:行者123 更新时间:2023-12-04 03:00:25 26 4
gpt4 key购买 nike

您好,我遇到了下面提到的错误。
我无法点击屏幕截图中提到的买方按钮。
我也试过等待、 sleep 功能。
但无法超越这一点。任何人都可以帮助我。

任何人都可以帮我解决这个问题:
Inspect Element code is .

代码是:

driver.findElement(By.name("login")).click();  //Click on login button
System.out.println("hello world-----4");

try {
Thread.sleep(5000);
} catch (InterruptedException e) {

e.printStackTrace();
}
System.out.println("hello world-----5");
WebElement element = driver.findElement(By.xpath("//*
[@id=\"modeuser\"]/div/ul/li[3]"));
((JavascriptExecutor)
driver).executeScript("arguments[0].scrollIntoView(true);", element);
element.click();
//Click on usertype



Error:

Exception in thread "main"
org.openqa.selenium.ElementNotInteractableException: Element <li
class="buyer_border changeusermode "> could not be scrolled into view
Build info: version: '3.9.0', revision: '698b3178f0', time: '2018-02-
05T14:56:13.134Z'
System info: host: 'CLAVAX-PC-93', ip: '192.168.2.122', os.name: 'Windows
10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_161'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox,
browserVersion: 58.0.2, javascriptEnabled: true, moz:accessibilityChecks:
false, moz:headless: false, moz:processID: 14260, moz:profile:
C:\Users\Rahul\AppData\Loca..., moz:webdriverClick: true, pageLoadStrategy:
normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable:
false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}


HTML is :

<div class="right hide-on-med-and-down head_right_mar" id="modeuser">
<!-- <div class="toggleWrapper">
<input class="dn" type="checkbox" id="dn" value="1"/>
<label class="toggle" for="dn"><span class="toggle__handler"></span></label>
</div> -->

<div class="right_toggle">
<ul>
<li data-get="seller" class="changeusermode active">
<span>Seller</span>
<span class="nav_span">On</span>

</li>


<li class="mid_toggle">
<div class="switch">
<label>

<input class="changeusermode_btn" type="checkbox" data-on="Yes" data-off="No">
<span class="lever"></span>
</label>
</div>
</li>


<li data-get="buyer" class="buyer_border changeusermode ">
<span>Buyer</span>

<span class="nav_span">Off</span>

</li>
</ul>
</div>


</div>

最佳答案

首先,验证元素在您的框架中 .

如果不是,您将需要切换到正确的框架才能单击元素:

driver.switchTo().frame(driver.findElement(By.name("iframeWithElement")));

此外,您可以执行许多步骤来提高单击不同 UI 元素时的稳定性:
  • 明确 等待它的存在在 DOM 中
  • 滚动 进入元素 View
  • 检查是否 可点击

  • 对稳定性有帮助吗?
    WebDriverWait wait = new WebDriverWait(driver, 3);
    JavascriptExecutor js = ((JavascriptExecutor) driver);

    //presence in DOM
    wait.until(ExpectedConditions.presenceOfElement(By.id("ID")));

    //scrolling
    WebElement element = driver.findElement(By.id("ID")));
    js.executeScript("arguments[0].scrollIntoView(true);", element);

    //clickable
    wait.until(ExpectedConditions.elementToBeClickable(By.id("ID")));

    例如,如果我正在处理 site ,我将使用:
    Wait.until(ExpectedConditions.presenceOfElement(By.class("article-feed-title")));

    关于Selenium:该元素无法滚动到 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49045221/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com