如何从列表中按名称获取项目
How can get an item by its name from the list
我需要 select 列表中的项目名称。我尝试使用 "Select",但收到 "Element is not currently visible and so may not interacted with" 的错误。以下是我使用 Select:
的脚本
driver.findElement(By.xpath(".//*[@id='miscinfo_div']/ol/li[1]/div/fieldset/div/div[2]/div/div/ul/li"));
Select select = new Select(driver.findElement(By.id("companionPositionSelect")));
select.selectByVisibleText("Bottom1-Top1");
我也试过用List。它 select 通过其索引对项目进行排序,但我需要 select 仅通过其文本对项目进行排序。我想 List 没有通过文本 select 项目的选项。这是我的列表脚本:
List<WebElement> availableCompPositions = driver
.findElements(By
.xpath("//input[@id='companionPositionSelect_a_search']//following::div[1]/ul/li"));
// select Bottom1-Top1 and add in the SELECTED menu
availableCompPositions.get(4).click();
同时附上页面的 HTML。
HTML:
<div style="" id="miscinfo_div" class="toggler">
<ol>
<li>
<span>17.</span>
<label class="shufflelabel">
<div id="companionPositions_label">
<b> Companion Positions </b>
</div>
</label>
<div>
<fieldset class="shuttle_fieldset">
<legend></legend>
<div class="shuffle-box">
<select multiple="" id="companionPositionSelect" class="select2side" name="companionPositionSelect" style="display: none;">
<option value="20408" title="Bottom-BottomLeft-BottomRight">Bottom-BottomLeft-BottomRight</option>
<option value="20391" title="Bottom-Middle-Top">Bottom-Middle-Top</option>
<option value="20382" title="Bottom-Top" selected="selected">Bottom-Top</option>
<option value="20392" title="Bottom1-Middle1-Top1" selected="selected">Bottom1-Middle1-Top1</option>
<option value="20383" title="Bottom1-Top1">Bottom1-Top1</option>
<option value="20393" title="Bottom2-Middle2-Top2">Bottom2-Middle2-Top2</option>
</select>
</div>
查看错误,我假设您过早地尝试执行操作。以下程序应明确等待最多 10 秒才能找到元素
已添加explicit
wait等待元素存在
//explicit wait
By byCss = By.cssSelector("#companionPositionSelect>option[value='20383']");
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(byCss));
element.click();
我需要 select 列表中的项目名称。我尝试使用 "Select",但收到 "Element is not currently visible and so may not interacted with" 的错误。以下是我使用 Select:
的脚本driver.findElement(By.xpath(".//*[@id='miscinfo_div']/ol/li[1]/div/fieldset/div/div[2]/div/div/ul/li"));
Select select = new Select(driver.findElement(By.id("companionPositionSelect")));
select.selectByVisibleText("Bottom1-Top1");
我也试过用List。它 select 通过其索引对项目进行排序,但我需要 select 仅通过其文本对项目进行排序。我想 List 没有通过文本 select 项目的选项。这是我的列表脚本:
List<WebElement> availableCompPositions = driver
.findElements(By
.xpath("//input[@id='companionPositionSelect_a_search']//following::div[1]/ul/li"));
// select Bottom1-Top1 and add in the SELECTED menu
availableCompPositions.get(4).click();
同时附上页面的 HTML。
HTML:
<div style="" id="miscinfo_div" class="toggler">
<ol>
<li>
<span>17.</span>
<label class="shufflelabel">
<div id="companionPositions_label">
<b> Companion Positions </b>
</div>
</label>
<div>
<fieldset class="shuttle_fieldset">
<legend></legend>
<div class="shuffle-box">
<select multiple="" id="companionPositionSelect" class="select2side" name="companionPositionSelect" style="display: none;">
<option value="20408" title="Bottom-BottomLeft-BottomRight">Bottom-BottomLeft-BottomRight</option>
<option value="20391" title="Bottom-Middle-Top">Bottom-Middle-Top</option>
<option value="20382" title="Bottom-Top" selected="selected">Bottom-Top</option>
<option value="20392" title="Bottom1-Middle1-Top1" selected="selected">Bottom1-Middle1-Top1</option>
<option value="20383" title="Bottom1-Top1">Bottom1-Top1</option>
<option value="20393" title="Bottom2-Middle2-Top2">Bottom2-Middle2-Top2</option>
</select>
</div>
查看错误,我假设您过早地尝试执行操作。以下程序应明确等待最多 10 秒才能找到元素
已添加explicit
wait等待元素存在
//explicit wait
By byCss = By.cssSelector("#companionPositionSelect>option[value='20383']");
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(byCss));
element.click();