【问题标题】:Selenium and Java: How do I get all of the text after a WebElementSelenium 和 Java:如何在 WebElement 之后获取所有文本
【发布时间】:2018-07-01 04:38:55
【问题描述】:

我正在使用 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,并且能够在搜索栏中输入内容。但是,我无法获得该条目产生的文本。

Image

在这里的图片中,我在搜索栏中输入了“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");

}

}

【问题讨论】:

  • 你能检查一下图像吗?我们无法访问图片。
  • 很抱歉。你能看看我现在发布的图片吗?这是“在此处输入图像描述”链接。谢谢!
  • 那么,您想从显示的 3 框中获取所有内容吗?
  • 是的,这就是我的目标。
  • 好的。我已经更新了答案。请检查

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

我已经指定了三种从结果框中提取文本的方法。请检查所有方法并使用所需的方法。

  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);
}

getText 方法

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());
}

我已经用上面修改过的代码进行了测试,它工作正常。

【讨论】:

  • 谢谢!但是,我有一个小问题。 System.out.println 没有向我的控制台打印任何内容。我在mac上使用eclipse。你知道为什么吗?
  • 哦...我正在使用 Windows Eclipse 并没有任何问题地获得结果。您是否在控制台中获得了至少“方法 1:”?
  • 不,这也不起作用。但是,其他 system.out 语句正在工作。例如,在调用执行这些步骤并应该打印某些内容的方法之后的 main 方法中,我打印了“hello”并出现在控制台窗口中。所以,我有点困惑为什么这不起作用。
  • 我怀疑这个方法可能不会被调用。你能把调用方法和其他sysout语句分享给我吗?
  • 可以参考一下我的原帖吗?我刚刚对其进行了编辑以包含我目前拥有的代码。
【解决方案2】:

为了检查查询的相关结果,常见的策略是加载搜索结果列表:

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]++;
});

你会得到输出:

【讨论】:

  • 非常感谢!!我会试试这个,希望它会奏效。
猜你喜欢
  • 2020-12-11
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
相关资源
最近更新 更多