gpt4 book ai didi

java - 无法使用 selenium webdriver 找到下拉值

转载 作者:行者123 更新时间:2023-12-01 11:24:47 25 4
gpt4 key购买 nike

我想从下拉列表中选择值。

目前我可以单击下拉列表,但无法从下拉列表中选择值。下面是我用来从下拉列表中选择值的代码。

                temp.click();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
Select clickThis = new Select(temp);
try{

Thread.sleep(5000);
clickThis.selectByValue("India");

}catch(Exception e){
e.printStackTrace();
System.out.println("<><><><><>Not Found<><><><><><>");
}

我正在框架中工作,您能让我知道相应的代码吗?请检查我正在使用的以下代码。

private boolean operateWebDriver(String operation, String Locator,
String value, String objectName) throws Exception {
boolean testCaseStep = false;

try {
System.out.println("Operation execution in progress");
WebElement temp = getElement(Locator, objectName);
if (operation.equalsIgnoreCase("SendKey")) {
temp.sendKeys(value);
}
Thread.sleep(1000);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
if (operation.equalsIgnoreCase("Click")) {
temp.click();
}
if (operation.equalsIgnoreCase("Verify")) {
System.out.println("Verify--->" + temp);
temp.isDisplayed();

}
if (operation.equalsIgnoreCase("clickDropdown")) {

temp.click();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
Select clickThis = new Select(temp);
try{

Thread.sleep(5000);
clickThis.selectByValue("India");

}catch(Exception e){
e.printStackTrace();
System.out.println("<><><><><>Not Found<><><><><><>");
}
}

testCaseStep = true;

} catch (Exception e) {
System.out.println("Exception occurred operateWebDriver"
+ e.getMessage());

// Take screenshot if any testcase is not working.
System.out.println("Taking Screen Shot");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("E:\\workspace for selenium\\Simple page creator\\Snapshot\\screenshot.jpeg"));
}

return testCaseStep;
}

public WebElement getElement(String locator, String objectName)
throws Exception {
WebElement temp = null;

System.out.println("Locator-->" + locator);
if (locator.equalsIgnoreCase("id")) {
temp = driver.findElement(By.id(objectName));

} else if (locator.equalsIgnoreCase("xpath")) {
temp = driver.findElement(By.xpath(objectName));
System.out.println("xpath temp ----->" + temp);
} else if (locator.equalsIgnoreCase("name")) {
temp = driver.findElement(By.name(objectName));
}
return temp;

}

}

HTML

<select id="billing_country" name="billing_country">
<option value="">Choose Country</option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="AG">Algeria</option>
<option value="AQ">American Samoa</option>
<option value="AN">Andorra</option>
<option value="AO">Angola</option>
<option value="AV">Anguilla</option>
<option value="AY">Antarctica</option>
<option value="AC">Antigua and Barbuda</option>
<option value="AR">Argentina</option>
<option value="AM">Armenia</option>
<option value="AA">Aruba</option>
<option value="AT">Ashmore and Cartier</option>
<option value="AS">Australia</option>
<option value="AU">Austria</option>
<option value="AJ">Azerbaijan</option>
<option value="BF">The Bahamas</option>
<option value="BA">Bahrain</option>
<option value="FQ">Baker Island</option>
<option value="BG">Bangladesh</option>

最佳答案

通过 HTML 代码,我假设 India 是选项的文本,如下所示:

<option value="IN">India</option> 

对于这种情况,请尝试使用以下与 Saifur 的回复类似的代码,但方法不同。

By element = driver.findElement(By.id("billing_country"));
Select foo = new Select(element);
foo.selectByVisibleText("India");

如果您想使用您的框架,请分享 webdriver 抛出的异常。由于国家/地区列表太大,网络驱动程序单击时可能看不到“印度”选项,并且会引发类似于以下内容的异常:org.openqa.selenium.WebDriverException:元素在点 (XXX, YYY) 处不可单击。其他元素将收到点击:

关于java - 无法使用 selenium webdriver 找到下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30916328/

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