簡體   English   中英

Selenium 和 Java:如何在 WebElement 之后獲取所有文本

[英]Selenium and Java: How do I get all of the text after a WebElement

我正在使用 WebDriver 在 Java 中編寫程序,並且在選擇 webElement 之后獲取文本時遇到了一些麻煩。

我想要的網站部分的 HTML 代碼如下:

    <select name="language" id="langSelect" style="width:100px;">
            <option value="1"  >Français</option>
    </select>
  </div>

  <div id="content">

    
            <div id="Pagination"></div>
    <div id="mid">
              </div>
  </div>

搜索欄和語言下拉欄的文本框類代碼

我的 Java 代碼目前可以使用 chrome 驅動程序打開 chrome,並且可以在搜索欄中輸入內容。 但是,我無法獲得條目產生的文本。

圖片

在此處的圖像中,我在搜索欄中輸入了“avoir”,並且我希望框中的所有文本之后似乎沒有在 xpath 中使用的任何 id 或名稱。

有人可以幫我找到如何在下拉語言菜單后從這些字段中獲取和保存文本嗎?

先感謝您!

我到目前為止的代碼:

//import statements not shown
    public class WebScraper  {
    public WebScraper() {
    
    }

    public WebDriver driver = new ChromeDriver();

    public void openTestSite() {
   
        driver.navigate().to(the URL for the website);
    }


    public void enter(String word) {

         WebElement query_editbox = 
         driver.findElement(By.id("query")); 
         query_editbox.sendKeys(word);
         query_editbox.sendKeys(Keys.RETURN);

    }

    public void getText()  {
        //List<WebElement> searchResults = 
        driver.findElements(By.xpath("//div[@id='mid']/div")); 
        // Writer writer = new BufferedWriter(new 
        OutputStreamWriter(new FileOutputStream("status.txt"), 
        "utf-8"));
        //int[] index = {0};

    WebElement result=driver.findElement(By.id("mid"));
    System.out.println(result.getText());
}

public static void main(String[] args) throws IOException  {
    System.setProperty("webdriver.chrome.driver", "chromedriver");        
    System.out.println("Hello");

    WebScraper webSrcaper = new WebScraper();
    webSrcapper.openTestSite();
    webSrcapper.enter("avoir");
    webSrcapper.getText();
    System.out.println("Hello");

}

}

為了檢查查詢的相關結果,常見的策略是加載搜索結果列表:

List<WebElement> searchResults = driver.findElements(By.xpath("//div[@id='mid']/div")); 

現在,您可以使用流來遍歷列表並提取相關文本,方法是從每個結果的子元素中獲取文本:

int[] index = {0};
searchResults.stream().forEach(result -> {
    System.out.println("Printing query result of index: " + index[0]);
    result.findElements(By.xpath(".//*")).stream().forEach(webElement -> {
        try {
            System.out.println(webElement.getText());
        } catch (Exception e) {
            // Do nothing
        }
    });
    index[0]++;
});

你會得到輸出:

在此處輸入圖片說明

我指定了三種方法來從結果框中提取文本。 請檢查所有方法並使用所需的方法。

  1. 如果要提取所有文本,則可以找到結果框的元素,然后從中獲取文本。

     WebElement result=driver.findElement(By.id("mid")); System.out.println(result.getText());
  2. 如果您想根據逐節提取文本,則可以使用以下方法,

     List<WebElement> sectionList=driver.findElements(By.xpath("//div[@id='mid']/div")); int i=0; for(WebElement element:sectionList){ System.out.println("Section "+i+":"+element.getText()); i++; }
  3. 如果要從特定部分提取文本,則可以使用以下方法

     List<WebElement> sectionList=driver.findElements(By.xpath("//div[@id='mid']/div")); int i=0; //Inorder to get the Section 3 Content int section=2; for(WebElement element:sectionList){ if(section==i){ System.out.println("Section "+i+":"+element.getText()); } i++; }

編輯:解決后續問題

我建議在執行一些導致某些元素呈現的操作后使用一些顯式等待。 在您的代碼中,經過一些修改后,我得到了預期的結果。

  1. openTestSite方法中,我剛剛添加了顯式等待以確保加載 URL 后頁面加載
  2. enter方法中,實際上您在輸入查詢值后會得到自動完成建議。因此,我們只需要從自動完成中選擇值即可。
  3. getText方法中,搜索結果需要更多時間。因此,我們需要使用任何一種動態加載元素定位器添加一些顯式等待。

代碼:

openTestSite 方法:

    public void openTestSite() {

    //driver.navigate().to(the URL for the website);
    driver.get("https://wonef.fr/try/");
    driver.manage().window().maximize();
    //Explicit wait is added after the Page load
    WebDriverWait wait=new WebDriverWait(driver,20);
    wait.until(ExpectedConditions.titleContains("WoNeF"));
}

輸入方法:

public void enter(String word) {

    WebElement query_editbox =
            driver.findElement(By.id("query"));
    query_editbox.sendKeys(word);
    //AutoComplete is happening even after sending the Enter Key.
    // So, Value needs to be selected from the autocomplete
    WebDriverWait wait=new WebDriverWait(driver,20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='autocomplete']/div")));
    List<WebElement> matchedList=driver.findElements(By.xpath("//div[@class='autocomplete']/div"));

    System.out.println(matchedList.size());
    for(WebElement element : matchedList){
        if(element.getText().equalsIgnoreCase(word)){
            element.click();
        }
    }
    //query_editbox.sendKeys(Keys.RETURN);
}

獲取文本方法

public void getText()  {
    WebDriverWait wait=new WebDriverWait(driver,20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='mid']/div")));
    WebElement result=driver.findElement(By.id("mid"));
    System.out.println(result.getText());
}

我已經用上面修改過的代碼進行了測試,它工作正常。

暫無
暫無

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

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