首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法从下拉菜单中选择值

无法从下拉菜单中选择值
EN

Stack Exchange QA用户
提问于 2022-04-11 06:41:18
回答 3查看 566关注 0票数 -1

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>中的下拉菜单的值。

EN

回答 3

Stack Exchange QA用户

发布于 2022-04-15 04:57:01

创建Select的方式需要更改

代码语言:javascript
运行
复制
Select countrySel=new Select(driver.findElement(By.name("country")));
countrySel.selectByValue("Antarctica");

我希望这能帮上忙。

票数 0
EN

Stack Exchange QA用户

发布于 2022-04-30 13:59:42

就问题陈述而言,我认为我们有两种不同的要求:

  1. 打印包含值的列表。
  2. 取值=“南极洲”从乡下掉下来。

//代码

代码语言:javascript
运行
复制
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);
        } */ 
                
    }
}

我希望这能满足你的期望。谢谢。

票数 0
EN

Stack Exchange QA用户

发布于 2022-05-20 05:29:36

自动化测试服务公司中的每一个qa人都面临着这种情况。

首先,由于下拉标签是Select,所以可以直接使用selectByValueselectByVisibleText来选择值,如下所示,无需单击下拉列表:

代码语言:javascript
运行
复制
Select s = new Select(driver.findElement(By.name("country")));

s.selectByValue("Antarctica");

其次,要捕获下拉值,请使用以下代码:

代码语言:javascript
运行
复制
  // 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);
  }
票数 0
EN
页面原文内容由Stack Exchange QA提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://sqa.stackexchange.com/questions/49991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档