https://demo.guru99.com/test/newtours/register.php
我试图自动化这个页面上的下拉国家菜单,但无法从下拉菜单中选择任何值。
Eclipse正在抛出以下错误:
线程"main“中的异常:‘org.openqa.selenium.WebElement.getDomAttribute(java.lang.String)’at org.openqa.selenium.support.ui.Select.(Select.java:54) at ui.DropDown1.main(DropDown1.java:23)
我可以点击下拉菜单,但它没有选择任何值。它也没有打印存储在List<WebElements>
中的下拉菜单的值。
发布于 2022-04-15 04:57:01
创建Select的方式需要更改
Select countrySel=new Select(driver.findElement(By.name("country")));
countrySel.selectByValue("Antarctica");
我希望这能帮上忙。
发布于 2022-04-30 13:59:42
就问题陈述而言,我认为我们有两种不同的要求:
//代码
package TestScripts;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class selectValue {
//Object declaration
public static FirefoxDriver ffdr = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
// Creating instance of Firefox Driver
ffdr = new FirefoxDriver();
// to open given URL using firefox driver with get non static method
ffdr.get("https://demo.guru99.com/test/newtours/register.php");
System.out.println("Inside BeforeClass");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("Inside AfterClass end of program.");
//quits the entire browser session with all its tabs and windows
ffdr.quit();
}
@Test
public void test() {
/*To print out list you can do it in this way.
* */
List<WebElement> drop = ffdr.findElements(By.xpath("//select[@name='country']"));
System.out.println(drop.get(0).getText());
/*To select value we can easily identify by this way by name.
* */
WebElement cntry = ffdr.findElement(By.name("country"));
Select drpcntry = new Select(cntry);
drpcntry.selectByValue("ANTARCTICA");
/*
String[] words=drop.get(0).getText().toString().split("\\n");//splits the string based on New line
//using java for each loop to print elements of string array
for(String w:words){
System.out.println(w);
} */
}
}
我希望这能满足你的期望。谢谢。
发布于 2022-05-20 05:29:36
自动化测试服务公司中的每一个qa人都面临着这种情况。
首先,由于下拉标签是Select,所以可以直接使用selectByValue
、selectByVisibleText
来选择值,如下所示,无需单击下拉列表:
Select s = new Select(driver.findElement(By.name("country")));
s.selectByValue("Antarctica");
其次,要捕获下拉值,请使用以下代码:
// getting the list of options in the dropdown with getOptions()
List <WebElement> op = s.getOptions();
int size = op.size();
for(int i =0; i<size ; i++){
String options = op.get(i).getText();
System.out.println(options);
}
https://sqa.stackexchange.com/questions/49991
复制相似问题