簡體   English   中英

Selenium - 使用Java單擊下拉列表並選擇一個值

[英]Selenium - Using Java to click on a dropdown list and select a value

我正在嘗試連續自動下載大量指標。 到目前為止,我可以打開URL,登錄,切換選項卡,然后轉到所需的部分。 我在點擊下拉列表並選擇一個值以將列表從10擴展到100時遇到問題。

到目前為止,這是我的代碼

SoltraDownload.java

import java.util.Scanner;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class SoltraDownload {

public static void main(String[] args) {

    //PROMPT USER FOR Password
    //System.out.print("Please Enter Your Username: ");
    //Scanner scanner = new Scanner(System.in);
    //String username = scanner.next();

    //PROMPT USER FOR Username
    //System.out.print("Please Enter Your Password: ");
    //String password = scanner.next();

    //GET WEB DRIVER
    System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("EDITED FOR QUESTION");
    driver.manage().window().maximize();    

    //GET INSTANCE OF Soltra HOME PAGE
    SoltraHomePage SoltraHomePage = new SoltraHomePage(driver);

    //LOGIN TO SOLTRA
    SoltraHomePage.loginP("EDITED FOR QUESTION");
    SoltraHomePage.loginU("EDITED FOR QUESTION");
    SoltraHomePage.loginC();

    //MOVE TO FEEDS PAGE
    SoltraHomePage.FeedPage();

    //PAGE CLICK ON FEED SELECTION
    SoltraHomePage.ChooseFeed();

    //SELECT DROPDOWN value 100
    SoltraHomePage.Dropdown();
}

}

SoltraHomePage.java

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;


public class SoltraHomePage {

@FindBy(how = How.ID, using = "username") private WebElement Username;
@FindBy(how = How.ID, using = "password") private WebElement Password;
@FindBy(how = How.ID, using = "loginbutton") private WebElement loginbutton;
@FindBy(how = How.LINK_TEXT, using = "Feeds") private WebElement Feeds;
@FindBy(how = How.ID, using = "feedlist") private WebElement feedlist;
@FindBy(how = How.CLASS_NAME, using = "container") private WebElement container;
@FindBy(how = How.LINK_TEXT, using = "Default") private WebElement Default;
@FindBy(how = How.CSS, using = "control-bar") private String control;

// Create a new instance of a driver
WebDriver driver = new HtmlUnitDriver();

public SoltraHomePage(WebDriver driver) {
    PageFactory.initElements(driver, this);
}

public void loginU(String text) {
    this.Username.clear();
    this.Username.sendKeys(text);
}

public void loginP(String text) {
    this.Password.clear();
    this.Password.sendKeys(text);
}

public void loginC(){
    this.loginbutton.click();
}

public void FeedPage() {
    this.Feeds.click();
}

public void ChooseFeed() {
    feedlist.findElement(By.partialLinkText("Default")).click();
}

public void Dropdown() {
    //Code to select dropdown value
}
}

這是html代碼的一個子集 - 我希望在下拉列表中選擇值100

<div class="row control-bar">

<div class="col-sm-4"></div>

<div class="col-sm-4">

<ul class="pager">

<li class="previous"><input type="button" class="btn btn-default" data-    bind="click: goFirst, disable: page()==1" value="&lt;&lt;"></li>

<li class="previous"><input type="button" class="btn btn-default" data-  bind="click: goPrev, disable: page()==1" value="&lt;"></li>

<li><small data-bind="text: page"></small> of <small data-bind="text: totalPages"></small> (<small data-bind="text: totalRows"></small>

<small data-bind="if: totalRows()!==1">rows</small><small data-bind="if: totalRows()==1">row</small>)</small></li>

<li class="next"><button class="btn btn-default" data-bind="click: goNext, disable: page() == totalPages()">&gt;</button></li>

<li class="next"><button class="btn btn-default" data-bind="click: goLast, disable: page() == totalPages()">&gt;&gt;</button></li>

</ul>
</div>
    <div class="col-sm-4">
    <ul class="pager">
        <li class="pull-right"><small>Indicators per page: </small>
            <select data-bind="value: pageSize, event: { change: loadData }" style="width:auto">
                <option value="5">5</option>
                <option value="10">10</option>
                <option value="20">20</option>
                <option value="50">50</option>
                **<option value="100">100</option>** //This is the value I am looking to choose
        </select>
    </li>
</ul>

我總是創建一個自定義動作類並將它們放在那里:

public static void selectByValue(WebElement element, String value) {
    Select selectElement = new Select(element);
    selectElement.selectByValue(value);
}
public static void selectByText(WebElement element, String text) {
    Select selectElement = new Select(element);
    selectElement.selectByVisibleText(text);
}

這些將獲取WebElement,將其轉換為Select元素(org.openqa.selenium.support.ui.Select),並使用Select方法選擇您的選項。 這僅適用於html選擇元素。 你會像這樣使用它:

在頂部:

@FindBy(how = How.CSS, using = "li.pull-right select") private WebElement soltraSelect;

在測試中:

CustomActions.selectByValue(soltraSelect, "100");

您可以使用Select對象或定義正確的XPath:

1)使用選擇:

Select element = new Select(webelement);
element.selectByValue(value);

2)使用XPath:

By option = By.xpath("//select[@class = 'expression']/option[@value = 'value']");
driver.findElement(option).click();

由於value屬性與文本具有相同的值,我建議使用以下cssSelector以獲得更好的搜索結果

String val = "100";
By css = By.cssSelector("select>option[value='" + val + "']");
driver.findElement(css).click();

另一方面,您可以使用selectByValue方法來完成相同的事情而不是視覺文本。 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM