使用相同的" id"验证下拉框。

时间:2017-10-09 12:40:41

标签: java selenium

我在这里遇到问题,我试图使用相同的" id"来自动化下拉框。 。 Ther是三个这样的Dropdown盒子。我必须在每个选项下选择一个选项。救命 ? 网站:http://bookboon.com/en/basics-of-accounting-information-processing-ebook#download

package flow;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class bookboon {
    static WebDriver d  ;
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\User_2\\Downloads\\chromedriver_win32\\chromedriver.exe");
        d = new ChromeDriver();
        d.get("http://bookboon.com/en/accounting-ebooks");
        List<WebElement> downlinks = d.findElements(By.className("pdf"));
        for(int i=1;i<=downlinks.size();i++) {
            downlinks.get(i).click();
            d.findElement(By.id("email")).sendKeys("asd@ymail.com");
            d.findElement(By.id("undefined_flexselect")).sendKeys("Studying");
            d.findElement(By.id("undefined_flexselect")).sendKeys("Engineer/Science MSc");
            d.findElement(By.id("undefined_flexselect")).sendKeys("All India Institute of Medical Sciences (AIIMS), Delhi");
            //d.navigate().back();
            //downlinks = d.findElements(By.className("pdf"));
        }
    }
}

我也试过Xpath。无法完成任务。请帮忙 !

3 个答案:

答案 0 :(得分:1)

您可以使用以下选项&#39; nth&#39; xpath值。

"(//elementType[@id='undefined_flexselect'])[n]"

将括号中的n替换为您要选择的记录编号。

答案 1 :(得分:0)

您可以组合两个属性来标识元素:

d.findElement(By.xpath(".//select[@name='answers' and @data-placeholder='Studying or working?']")).sendKeys("Studying");

或者采取所有这些,然后采取每一个:

List<WebElement> a = driver.findElements(By.xpath(".//select[@name='answers']"));
a.get(0).sendKeys("Studying");

答案 2 :(得分:-1)

试试这个..

d.findElement(By.id("email")).sendKeys("asd@ymail.com");
WebElement fOne=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[1]/input"));
fOne.sendKeys("Studying");
fOne.sendKeys(Keys.TAB);
Sleeper.sleepTightInSeconds(2);
WebElement fTwo=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[2]/input"));
fTwo.sendKeys("Engineer/Science MSc");
fTwo.sendKeys(Keys.TAB);
Sleeper.sleepTightInSeconds(2);
WebElement fThree=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[3]/input"));
fThree.sendKeys("All India Institute of Medical Sciences (AIIMS), Delhi");
fThree.sendKeys(Keys.TAB);