gpt4 book ai didi

java - 如何在 Selenium WebDriver 中将 Dropdown 与 ng-model 一起使用?

转载 作者:行者123 更新时间:2023-12-01 09:31:36 25 4
gpt4 key购买 nike

我正在尝试使用 angularjs ng-model 为我的 Selenium WebDriver 代码使用下拉菜单,但它不起作用。我的代码是。

package automation.test;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","../geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:/Program Files/Mozilla Firefox/firefox.exe");
DesiredCapabilities capabilities=DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
String baseUrl = "http://abc/bluestar/Admin";

driver.get(baseUrl);
driver.findElement(By.id("btnLogin")).click();
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Rough Transaction"))).click();
driver.findElement(By.cssSelector("a[href='/bluestar/Admin/RoughTransaction/RoughInward']")).click();
Select dropdown = new Select(driver.findElement(By.xpath("//*[@ng-model='RoughInward.TransationModeId']")));
dropdown.selectByVisibleText("LOCAL PURCHASE ");
/*WebElement webElement = driver.findElement(By.xpath("//*[@ng-model='TransationModeId']"));
Select select = new Select(webElement);
select.selectByVisibleText("LOCAL PURCHASE");*/
// wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("LogOut"))).click();
driver.close();
System.out.println("Done");
}
}

显示以下错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@ng-model='RoughInward.TransationModeId'] (WARNING: The server did not provide any stacktrace information)

HTML 是

<select class="form-control MinWith input-sm ng-pristine ng-invalid ng-invalid-required ng-touched" ng-model="RoughInward.TransationModeId" required="" autofocus="" ng-options="item.id as item.value for item in TransactionModeS" style="width: 100%;">
<option selected="selected" class="" value="">Select Transaction Mode</option>
<option label="IMPORT" value="number:12">IMPORT</option>
<option label="LOCAL PURCHASE" value="number:13">LOCAL PURCHASE</option>
<option label="CONSIGNMENT PURCHASE" value="number:15">CONSIGNMENT PURCHASE</option><option label="MEMO PURCHASE" value="number:18">MEMO PURCHASE</option>
</select>

如何解决?

最佳答案

尝试在驱动程序初始化后添加隐式等待

WebDriver driver = new FirefoxDriver(capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

这将尝试定位元素长达 10 秒。

您也可以尝试通过部分ng-model来定位

driver.findElement(By.cssSelector("[ng-model*='TransationModeId']"));

关于java - 如何在 Selenium WebDriver 中将 Dropdown 与 ng-model 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39362087/

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