日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java如何读取下拉列表的值_java - 如何在Selenium 2中选择/获取下拉选项

發(fā)布時(shí)間:2024/9/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java如何读取下拉列表的值_java - 如何在Selenium 2中选择/获取下拉选项 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

java - 如何在Selenium 2中選擇/獲取下拉選項(xiàng)

我正在將我的selenium 1代碼轉(zhuǎn)換為selenium 2,并且無法找到在下拉菜單中選擇標(biāo)簽的任何簡單方法或獲取下拉列表的選定值。 你知道如何在Selenium 2中做到這一點(diǎn)嗎?

以下是兩個(gè)在Selenium 1中有效但在2中不起作用的語句:

browser.select("//path_to_drop_down", "Value1");

browser.getSelectedValue("//path_to_drop_down");

8個(gè)解決方案

183 votes

查看有關(guān)使用selenium文檔中的webdriver和Select類的javadoc填寫表單的部分。

要根據(jù)標(biāo)簽選擇選項(xiàng):

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));

select.deselectAll();

select.selectByVisibleText("Value1");

要獲得第一個(gè)選定值:

WebElement option = select.getFirstSelectedOption()

janderssn answered 2019-08-11T12:22:16Z

5 votes

driver.findElement(By.id("id_dropdown_menu")).click();

driver.findElement(By.xpath("xpath_from_seleniumIDE")).click();

祝好運(yùn)

thrasher answered 2019-08-11T12:22:36Z

4 votes

在紅寶石中不斷使用,添加如下:

module Selenium

module WebDriver

class Element

def select(value)

self.find_elements(:tag_name => "option").find do |option|

if option.text == value

option.click

return

end

end

end

end

end

你將能夠選擇價(jià)值:

browser.find_element(:xpath, ".//xpath").select("Value")

AlekseiPetrovski answered 2019-08-11T12:23:07Z

3 votes

嘗試使用:

selenium.select("id=items","label=engineering")

要么

selenium.select("id=items","index=3")

coolcub answered 2019-08-11T12:23:28Z

0 votes

與janderson上面發(fā)布的內(nèi)容類似的選項(xiàng)就是在selenium 2中使用.GetAttribute方法。使用此方法,您可以獲取具有您正在尋找的特定值或標(biāo)簽的任何項(xiàng)目。 這可用于確定元素是否具有標(biāo)簽,樣式,值等。執(zhí)行此操作的常用方法是循環(huán)顯示下拉列表中的項(xiàng)目,直到找到所需的項(xiàng)目并選擇它。 在C#中

int items = driver.FindElement(By.XPath("//path_to_drop_Down")).Count();

for(int i = 1; i <= items; i++)

{

string value = driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).GetAttribute("Value1");

if(value.Conatains("Label_I_am_Looking_for"))

{

driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).Click();

//Clicked on the index of the that has your label / value

}

}

Ben answered 2019-08-11T12:23:53Z

0 votes

你可以這樣做:

public void selectDropDownValue(String ValueToSelect)

{

webelement findDropDownValue=driver.findElements(By.id("id1")) //this will find that dropdown

wait.until(ExpectedConditions.visibilityOf(findDropDownValue)); // wait till that dropdown appear

super.highlightElement(findDropDownValue); // highlight that dropdown

new Select(findDropDownValue).selectByValue(ValueToSelect); //select that option which u had passed as argument

}

Praveen answered 2019-08-11T12:24:17Z

0 votes

此方法將返回下拉列表的選定值,

public static String getSelected_visibleText(WebDriver driver, String elementType, String value)

{

WebElement element = Webelement_Finder.webElement_Finder(driver, elementType, value);

Select Selector = new Select(element);

Selector.getFirstSelectedOption();

String textval=Selector.getFirstSelectedOption().getText();

return textval;

}

與此同時(shí)

String textval = Selector.getFirstSelectedOption();

element.getText();

將返回下拉列表中的所有元素。

Jophin P John answered 2019-08-11T12:25:10Z

-2 votes

這是從下拉列表中選擇值的代碼

selectlocator的值將是下拉框的xpath或名稱,而optionLocator將具有從下拉框中選擇的值。

public static boolean select(final String selectLocator,

final String optionLocator) {

try {

element(selectLocator).clear();

element(selectLocator).sendKeys(Keys.PAGE_UP);

for (int k = 0; k <= new Select(element(selectLocator))

.getOptions().size() - 1; k++) {

combo1.add(element(selectLocator).getValue());

element(selectLocator).sendKeys(Keys.ARROW_DOWN);

}

if (combo1.contains(optionLocator)) {

element(selectLocator).clear();

new Select(element(selectLocator)).selectByValue(optionLocator);

combocheck = element(selectLocator).getValue();

combo = "";

return true;

} else {

element(selectLocator).clear();

combo = "The Value " + optionLocator

+ " Does Not Exist In The Combobox";

return false;

}

} catch (Exception e) {

e.printStackTrace();

errorcontrol.add(e.getMessage());

return false;

}

}

private static RenderedWebElement element(final String locator) {

try {

return (RenderedWebElement) drivers.findElement(by(locator));

} catch (Exception e) {

errorcontrol.add(e.getMessage());

return (RenderedWebElement) drivers.findElement(by(locator));

}

}

謝謝,

雷卡。

Kartmcad answered 2019-08-11T12:25:50Z

總結(jié)

以上是生活随笔為你收集整理的java如何读取下拉列表的值_java - 如何在Selenium 2中选择/获取下拉选项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。