gpt4 book ai didi

java - 如何选择下拉选项

转载 作者:行者123 更新时间:2023-11-30 06:15:43 25 4
gpt4 key购买 nike

我想使用 Selenium 找到以下下拉选项,但我无法这样做。

<div class="customFonts" style="">
<div class="fontOptions"><div class="fontView">
<div class="fontDropDown">
<div class="styledSelect customSelect">
<select style="opacity: 0;">
<option value="Arial">Arial</option>
<option value="courier, monospace">Courier</option>
<option value="Geneva">Geneva</option>
<div class="toggleSwitchSliderInner">
<img src="images/toggle_circle.png" style="display: block; right: -3px;">
</div>
<option value="Helvetica">Helvetica</option><!--This option-->
<option value="Roboto">Roboto</option>
<option value="sans-serif">sans-serif</option>
<option value="sourceSansPro">SourceSansPro</option>
<option value="times new roman, serif">Times New Roman</option>
<option value="verdana, sans-serif">Verdana</option>
</select>
<span>Courier</span>
<div class="customSelectIcon"></div>
</div>
</div>

如何选择以下下拉选项。我不断收到无法找到元素的信息。请推荐

我尝试了以下两种变体:

List<WebElement> b = driver.findElements(
By.xpath("//div[@class='fontDropDown']"));
new Select(b.get(1).findElement(
By.xpath("//select[text()='Helvetica']")));

抛出无法找到元素。并且

List<WebElement> b = driver.findElements(
By.xpath("//div[@class='fontDropDown']"));
new Select(b.get(1).findElement(
By.xpath("//select/option[text()='Helvetica']")))

产生的元素应该是select,但是是option

最佳答案

您的第一种方法无法找到该元素,因为该值位于 option 内部,而不是 select 元素内部。

您的第二种方法更好,但您尝试将结果包装在 Select 中。这不起作用,因为该方法返回 option 字段的句柄。因此,通过不包装 Select 将其转换为 WebElement:

WebElement option = b.get(1).findElement(
By.xpath("//select/option[text()='Helvetica']"));
<小时/>

由于您想要选择此选项,因此您实际上应该专注于访问 Select 元素,然后使用其方法选择其中一个选项。因此,请查看documentation 选择

这里有两种变体:

Select select = new Select(b.get(1).findElement(By.tagName("select")));

// Select by value (the attribute)
select.selectByValue("Helvetica");

// Select by visible text (the text inside the tag)
select.selectByVisibleText("Helvetica");

关于java - 如何选择下拉选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49246839/

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