簡體   English   中英

使用硒的JavaScript下拉選擇自動化

[英]Drop-down selection automation using selenium for javascript

我需要從具有JavaScript的下拉列表中選擇一個值。 任何幫助深表感謝!

以下HTML:

<div class="popupMenuButton">
    <a id="_sawrl" bh="PML" _mid="_g7yog" class="awmenuLink" style="text-decoration:none" href="#">
        <nobr>
            FORMALIN VAPO TABS
            <img width="15" height="17" align="absmiddle" border="0" style="margin-bottom:1px;" src="/EasyCare-2.0/AribaWeb/ad/content/AWXDebugResourceActions/13/en_US/widg/arrowcolor.gif" alt="">
        </nobr>
    </a>
</div>

Java代碼

WebElement dropDownListBox = waitById("_sawrl");
Select clickThis = new Select(dropDownListBox);
clickThis.selectByVisibleText("FORMALIN VAPO TABS")

例外

Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "a"
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
System info: host: , os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.7.0_51'
Driver info: driver.version: unknown
    at org.openqa.selenium.support.ui.Select.<init>(Select.java:46)
    at ui.stores.UIMaterialRecepit.setBrandName(UIMaterialRecepit.java:71)
    at tc.stores.TCMaterialRecepit.receiveMaterial(TCMaterialRecepit.java:32)
    at tc.StartAutmation.main(StartAutmation.java:48)

作為例外說明:

Element should have been "select" but was "a"

您的dropDownListBox是錨元素。 <a id="_sawrl"... )。
new Select()僅接受<select>元素。

正如Cerbrus所說,您的元素不是真正的選擇。 您可以通過單擊處理此類內容。

假設您的“選擇”如下所示:

<div class="menubox-select AFGFLKJV">
    <div class="menuItem" row="1" >Info</div>
    <div class="menuItem" row="2" >Stuff</div>
    <div class="menuItem" row="3" >Teddy Bears</div>
</div>

然后,為了選擇:

driver.findElement(By.xpath("//div[contains(@class, 'menubox-select')]").findElement(By.xpath(".//*[contains(text(), 'Stuff')]").click();

對於自定義選擇框,這是很平常的事情。 但是,您可以省略使用element.findElement。 仍然在單擊選項之前,您需要觸發選項以使其可見,否則會出現異常。

我能夠找出問題所在。 謝謝@Cerbrus和@TEH的指針。

以下是Java代碼:

driver.findElement(By.xpath("//*[@id='_sawrl']").click();
driver.findElement(By.xpath("//*[@id='_immwib']").click();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM