从div的层中获取数据返回重复的文本SELENIUM JAVA



我正试图从分层的div中获取一些文本。2返回了正确的文本,但最后一个只是重复出现,请指出我在哪里犯了错误

public static void get_Data_() throws IOException {
List<WebElement> urls = driver.findElements(By.cssSelector("this_is_container_div"));
for(WebElement e: urls) {
List<WebElement> img_html = e.findElements(By.xpath("//my_img_xpath"));
List<WebElement> title_html = e.findElements(By.cssSelector(".my_title_selector"));
String num = urls.get(0).getAttribute("this_is_num");
String title = title_html.get(0).getText();
String image = img_html.get(0).getAttribute("src");
System.out.println( num +" " +title+" "+image);

因此,前两个(即"num"one_answers"title"(运行良好,但最后一个(即图像(重复出现。HTML代码应该是这样的:

<html>
<div num-data_1="this_is_container_div">
<span>src = "this_is_the_image_url_1"</span>
<span>num = "this_is_num_1"</span>
<div class="my_title_selector_1">title</div>
<span>src = "this_is_the_image_url_2"</span>
<span>num = "this_is_num_2"</span>
<div class="my_title_selector_2">title</div>
<span>src = "this_is_the_image_url_3"</span>
<span>num = "this_is_num_3"</span>
<div class="my_title_selector_3">title</div>
<span>src = "this_is_the_image_url_4"</span>
<span>num = "this_is_num_4"</span>
<div class="my_title_selector_4">title</div>
</>div>
</html>

这就是结果的例子:

  1. this_is_num_001,title _001,this_is_the_image_url_1
  2. this_is_num_002,title _002,this_is_the_image_url_1
  3. this_is_num_003,title _003,this_is_the_image_url_1
  4. this_is_num_004,title _004,this_is_the_image_url_1
公共静态void get_Data_((抛出IOException{List urls=driver.findElements(By.csSelector("this_is_contender_div"((;
for(WebElement e: urls) {
List<WebElement> img_html = e.findElements(By.xpath(".//my_img_xpath"));
List<WebElement> title_html = e.findElements(By.cssSelector(".my_title_selector"));
String num = e.getAttribute("this_is_num");
String title = title_html.get(0).getText();
String image = img_html.get(0).getAttribute("src");
System.out.println( num +" " +title+" "+image);

最新更新