セレンのJavaを使用して、指定されたli要素をクリックすることができません。

invzbl3:

私は、指定した上でクリックする必要がありますli:ドロップダウンリストを使用して要素All subject areasの上にウェブサイト問題は次のとおりです。指定をクリックすることはできませんliセレンを使用して要素。

例えば、私は次のように足すだ場合:

 String textInput = "Agricultural and Biological Sciences";  

 //...

 // open the dropdown so the options are visible
 driver.findElement(By.className("dropdown"));

 // Get all of the options of dropdown list
 WebElement ulElement  = driver.findElement(By.cssSelector("ul.dropdown-options.dropdown-element"));
 List<WebElement> opts = ulElement.findElements(By.xpath(".//li/a"));

そして、指定された選択しようとしているli要素を:

// Loop through the options and select the one that matches
        for (WebElement opt : opts) {
            if(opt.getText().equals(textInput)){
               opt.click();
               //...
            }
        }

条件は、単にプログラムによってスキップされます。

私のようなバリアントに変更した場合:

  // Loop through the options and select the one that matches
        for (WebElement opt : opts) {
            if(!opt.findElements(By.xpath("//*[contains(text(), '" + textInput + "')]")).isEmpty()) {
                opt.click();
                //...
            }
        }

条件は無視され、正常に渡したが、その後のプログラムは、ボタンをクリックしないと、リストが閉じられていません。

誰かがどのようにここでの問題を解決するために私をお勧めすることはできますか?

DebanjanB:

click()テキストを持つ要素の農業と生物科学(雑多)ドロップダウンから、あなたが誘導する必要がWebDriverWaitをするためにelementToBeClickable()、あなたは以下を使用することができます ロケータ戦略

  • XPathの

    driver.navigate().to("https://www.scimagojr.com/journalrank.php?country=UA&page=1");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[normalize-space()='All subject areas']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[normalize-space()='All subject areas']//following-sibling::ul[1]//li/a[normalize-space()='Agricultural and Biological Sciences']"))).click();
    
  • ブラウザスナップショット:

scimagojr

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=16166&siteId=1