gpt4 book ai didi

java - Selenium Firefox Webdriver NoSuchElement 链接异常 :

转载 作者:行者123 更新时间:2023-12-01 10:28:50 29 4
gpt4 key购买 nike

出于某种原因,在 Selenium WebDriver 中单击链接对我来说似乎不起作用,而在 Selenium IDE 中却工作得很好。

我在 Selenium IDE 上构建到 Selenium Webdriver 的所有其他“复制粘贴”内容似乎都有效。

异常(exception):

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3"}
Command duration or timeout: 10.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.50.1', revision: 'd7fc91b', time: '2016-01-29 19:04:49'
System info: host: 'LT1736', ip: '10.54.3.175', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_71'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=43.0.4, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 62532082-d8c1-4d22-8884-6e266449ca06
*** Element info: {Using=css selector, value=a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3}

代码:

public class CRUD_Technologie {
public WebDriver driver;

@Test(priority=1)
public void openApp() {
driver.get("http://localhost:8080/index.html");
}

@Test(priority=2)
public void login() {
driver.findElement(By.cssSelector("#usernameInput")).sendKeys("MxAdmin");
driver.findElement(By.cssSelector("#passwordInput")).sendKeys("1");
driver.findElement(By.cssSelector("#loginButton")).click();
}

@Test(priority=3)
public void selectUserAndGoToPageTechnologieOverview() {
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
driver.findElement(By.cssSelector("div.mx-demouserswitcher-toggle")).click();
driver.findElement(By.cssSelector("li[data-id='3']")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3"))).click();
}

@Parameters("browser")
@BeforeTest
public void beforeTest(String browser) {
if(browser.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver();
}
else {
driver = new HtmlUnitDriver();
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

}

@AfterTest
public void afterTest() {
driver.close();
}
}

链接元素的相关HTML:

<div id="mxui_widget_Navbar_0" class="navbar navbar-default mx-navbar mx-name-menuBar1 hidden-xs" tabindex="-1" data-mendix-id="150" style="background-color: black ! important;" widgetid="mxui_widget_Navbar_0">
<ul class="nav navbar-nav">
<li class="mx-navbar-item">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-0" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-0">
<span class="glyphicon glyphicon-home"/>
Home
</a>
</li>
<li class="mx-navbar-item dropdown">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-1">
<span class="glyphicon glyphicon-th-list"/>
Master Data Mendix
<b class="caret"/>
</a>
<ul class="dropdown-menu mx-navbar-submenu">
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-0" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-1-0">
<span class="glyphicon glyphicon-euro"/>
Rollen
</a>
</li>
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-1" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-1-1">
<span class="glyphicon glyphicon-modal-window"/>
Scherm type
</a>
</li>
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-2" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-1-2">
<span class="glyphicon glyphicon-book"/>
Story type
</a>
</li>
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-1-3">
<span class="glyphicon glyphicon-briefcase"/>
Technologie
</a>
</li>
</ul>
</li>
<li class="mx-navbar-item dropdown">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-2" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-2">
<span class="glyphicon glyphicon-th-list"/>
Master Data AFAS
<b class="caret"/>
</a>
<ul class="dropdown-menu mx-navbar-submenu">
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-2-0" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-2-0">
<span class="glyphicon glyphicon-glass"/>
Klanten
</a>
</li>
<li class="mx-navbar-subitem">
<a class="mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-2-1" href="#" data-item-id="159b6aef-dbc3-5b23-a735-cf99f8341771-2-1">
<span class="glyphicon glyphicon-list-alt"/>
Werkmaatschappij
</a>
</li>
</ul>
</li>
</ul>
</div>

最佳答案

(1) 首先,您没有像处理其他元素那样等待相同的元素

示例:-

您正在等待:-

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li[data-id='3']"))).click();

你没有等待,尝试做与你在代码的其余部分中所做的类似的事情

driver.findElement(By.cssSelector("a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3")).click();

(2) 如果您能够使用相同 CSS 定位器使用 IDE 单击,则尝试通过 JavascriptExecutor 单击。

JavascriptExecutor是Selenium Webdriver提供的接口(interface)

if (driver.findElements(By.cssSelector("a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3").size() != 0) {
WebElement element= driver.findElement(By.cssSelector("a.mx-name-159b6aef-dbc3-5b23-a735-cf99f8341771-1-3"))
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
}
else{
System.out.println("Element not present");
}

希望对你有帮助:)

关于java - Selenium Firefox Webdriver NoSuchElement 链接异常 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35205603/

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