gpt4 book ai didi

java - 无法选择隐藏的下拉菜单

转载 作者:行者123 更新时间:2023-12-02 12:52:40 26 4
gpt4 key购买 nike

我正在尝试从 Selenium Webdriver 的下拉列表中选择一个值。但每次我在控制台中都会遇到错误

这是我的网站 URL下面是元素的快照:

enter image description here

我尝试过以下代码,但它不起作用

Select dropdown= new Select(driver.findElement(By.xpath("//div[@on-dimension-select = 'selectQuantityDimension']/*[@role='combobox']")));
dropdown.selectByValue("4");

出现以下异常:-

Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "span" Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'

也尝试过这种方式:

driver.findElement(By.xpath("//div[@on-dimension-select = 'selectQuantityDimension']/*[@role='combobox']")).click();
Select dropdown= new Select(driver.findElement(By.xpath("//div[@on-dimension-select = 'selectQuantityDimension']/select/")));
dropdown.selectByValue("4");

这次错误是:

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //div[@on-dimension-select = 'selectQuantityDimension']/select/ because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[@on-dimension-select = 'selectQuantityDimension']/select/' is not a valid XPath expression.

这是相同的 HTML 代码:

 <div class="ng-isolate-scope ng-pristine ng-valid" ng-show="!docked" ng-model="variantOptionsModel" on-dimension-select="selectQuantityDimension" docked="docked">
<label>Quantity:</label>
<select class="accessory-option ng-pristine ng-valid" ng-model="model.selectedQuantity" ng-options="quantity for quantity in model.quantityDropDownValues track by quantity" ng-change="quantityClick()" data-qa-quantity-value="" style="display: none;">
<option value="1" selected="selected" label="1">1</option>
<option value="2" label="2">2</option>
<option value="3" label="3">3</option>
<option value="4" label="4">4</option>
<option value="5" label="5">5</option>
<option value="6" label="6">6</option>
</select>
<span id="" class="selectboxit-container selectboxit-container" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" aria-owns="">
<span id="" class="selectboxit accessory-option ng-pristine ng-valid selectboxit-enabled selectboxit-btn" name="" tabindex="0" unselectable="on" data-qa-quantity-value="">
<ul class="selectboxit-options selectboxit-list" tabindex="-1" role="listbox" aria-hidden="true" style="max-height: 217px; top: -217px; display: none;">
</span>
</div>

我不确定我做错了什么。

最佳答案

这是您问题的答案:

这里是工作代码块,导航到“https://www.o2.co.uk/shop/smartwatches/samsung/gear-s2/#contractType=nonconnected ”,计算 Quantity 下拉列表中的条目数,将输出打印到控制台,最后选择 Quantity as 4 并打印到控制台:

package demo;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Q44559302_dropdown_select {

public static void main(String[] args) {


String innerhtml = null;
System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.o2.co.uk/shop/smartwatches/samsung/gear-s2/#contractType=nonconnected");
driver.findElement(By.xpath("//form[@id='chooseTariffForm']//div[@class='quantiy-picker-wrapper']/div/span")).click();
List<WebElement> element_list = driver.findElements(By.xpath("//form[@id='chooseTariffForm']//div[@class='quantiy-picker-wrapper']/div/span/ul/li/a"));
System.out.println("Number of Elements : "+element_list.size());
for (int i=0; i<element_list.size(); i++)
{
WebElement my_element = element_list.get(i);
innerhtml = my_element.getText();

if(innerhtml.contentEquals("4"))
{
my_element.click();
break;
}


}
System.out.println("Value selected from Dropdown is : "+innerhtml);

}

}

如果这能回答您的问题,请告诉我。

关于java - 无法选择隐藏的下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44559302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com