所以我在这里有以下列表框的HTML代码:
<div role="listbox" aria-expanded="false" class="quantumWizMenuPaperselectEl docssharedWizSelectPaperselectRoot freebirdFormviewerViewItemsSelectSelect freebirdThemedSelectDarkerDisabled" jscontroller="YwHGTd" jsaction="click:cOuCgd(LgbsSe); keydown:I481le; keypress:Kr2w4b; mousedown:UX7yZ(LgbsSe),npT2md(preventDefault=true); mouseup:lbsD7e(LgbsSe); mouseleave:JywGue; touchstart:p6p2H(LgbsSe); touchmove:FwuNnf; touchend:yfqBxc(LgbsSe|preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd(LgbsSe); focus:AHmuwe; blur:O22p3e;b5SvAb:TvD9Pc;" jsshadow="" jsname="W85ice" aria-describedby="i.desc.709120473 i.err.709120473" aria-labelledby="i73">
<div jsname="LgbsSe" role="presentation">
<div class="quantumWizMenuPaperselectOptionList" jsname="d9BH4c" role="presentation">
<div class="quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption isSelected isPlaceholder" jsname="wQNmvb" jsaction="" data-value="" aria-selected="true" role="option" tabindex="0">
<div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div>
<content class="quantumWizMenuPaperselectContent exportContent">Auswählen</content>
</div>
<div class="quantumWizMenuPaperselectOptionSeparator" role="presentation"></div>
<div class="quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="140 cm" aria-selected="false" role="option" tabindex="-1">
<div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div>
<content class="quantumWizMenuPaperselectContent exportContent">140 cm</content>
</div>
<div class="quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="141 cm" aria-selected="false" role="option" tabindex="-1">
<div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div>
<content class="quantumWizMenuPaperselectContent exportContent">141 cm</content>
</div>
<div class="quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="142 cm" aria-selected="false" role="option" tabindex="-1">
<div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div>
<content class="quantumWizMenuPaperselectContent exportContent">142 cm</content>
</div>
<div class="quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="143 cm" aria-selected="false" role="option" tabindex="-1">
<div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div>
<content class="quantumWizMenuPaperselectContent exportContent">143 cm</content>
</div>
</div>
<div class="quantumWizMenuPaperselectDropDown exportDropDown" role="presentation"></div>
</div>
<div class="exportSelectPopup quantumWizMenuPaperselectPopup" jsaction="click:dPTK6c(wQNmvb); mousedown:uYU8jb(wQNmvb); mouseup:LVEdXd(wQNmvb); mouseover:nfXz1e(wQNmvb); touchstart:Rh2fre(wQNmvb); touchmove:hvFWtf(wQNmvb); touchend:MkF9r(wQNmvb|preventMouseEvents=true)" role="presentation" jsname="V68bde" style="display:none;"></div>
</div>
我正在编写一个程序,必须自动选择此列表框中的一个元素(例如&#34; 140 cm&#34;,&#34; 141 cm&#34;您在代码等中看到...) java的。我尝试使用以下代码访问列表框:
WebElement checkBox = driver.findElement(By.cssSelector("div[aria-labelledby*=i73]"));
CheckBox.click();
它有效,但我没有选择元素。我尝试使用&#34;选择&#34; -Command:
Select listbox = new Select(checkBox);
listbox.selectByVisibleText("140 cm");
但它没有用。你有什么建议吗?感谢所有帮我的人!感谢你们。 :)
答案 0 :(得分:0)
等待点击后,尝试
Actions action = new Actions(driver);
Duration aDuration = Duration.ofSeconds(1);
action.moveToElement(boxElement).pause(aDuration).click(boxElement).build().perform();
理由是,有时Javascript必须在元素在点击之前具有焦点时执行,以使其起作用。
虽然moveToElement应该滚动但我发现它通常不会。所以我在动作之前使用了一个显式的滚动。
/**
*
* @param webDriver
* @param elementToScrollIntoView
* @param blockOption - If true, the top of the element will be aligned to the top of the visible area of the scrollable ancestor.
* If false, the bottom of the element will be aligned to the bottom of the visible area of the scrollable ancestor
*/
public void explicitScrollIntoView(WebDriver webDriver, WebElement elementToScrollIntoView, boolean blockOption) {
final String scriptStr = "arguments[0].scrollIntoView(" + blockOption + ");";
((JavascriptExecutor) webDriver).executeScript(scriptStr, elementToScrollIntoView);
sleepResponsibly(500);
}
public void sleepResponsibly(int timeMillisecond){
try{
Thread.sleep(timeMillisecond);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
}
}
暂停和睡眠是为了让Javascript有时间完成。