【问题标题】:Need to capture data from multiple webpage using selenium需要使用 selenium 从多个网页中捕获数据
【发布时间】:2017-02-09 06:38:40
【问题描述】:

网页:http://www.forbes.com/companies/icbc/

package selenium;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.By.ByTagName;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ForbesTest {

WebDriver driver;
String url;


    @Before
    public void setUp() throws Exception {

        System.setProperty("webdriver.ie.driver","D:\\IEDriverServer_x64_2.53.1\\IEDriverServer.exe");
        driver=new InternetExplorerDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        url="http://www.forbes.com/companies/icbc/";
        driver.get(url);
            }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        driver.close();
    }

    @Test
    public void test() throws InterruptedException {
        Thread.sleep(10000);
        WebElement tab=driver.findElement(By.className("large"));
        Thread.sleep(1000);
        String text= tab.getText();
        System.out.println(text);

        WebElement col1=driver.findElement(By.tagName("dt"));
        //Thread.sleep(1000);
        String industry= col1.getText();
        if(industry.matches("Industry")){
            System.out.println(industry);

            WebElement col2=driver.findElement(By.tagName("dd"));
            //Thread.sleep(1000);
            String industryName= col2.getText();
            System.out.println(industryName);
        }
        String forbesWebsite= driver.getCurrentUrl();
        System.out.println(forbesWebsite);
       WebElement nextPage=driver.findElement(By.className("next-number")); 
       nextPage.click();
       driver.close();
}
    }

我想获取排名、公司、国家、销售额、销售额排名、利润、排名利润、资产、排名资产、市值、排名市值、行业、成立、公司网站、员工、总部城市、CEO 姓名、 Forbes.com 公司信息页面和年份

【问题讨论】:

  • 你的问题是......?
  • 我需要捕获 Industry、Federation 等,但它们具有相同的标签。我应该使用什么 XPath?如果是 XPath 我怎么得到它?

标签: java excel selenium


【解决方案1】:

获取行业文本:

String industryName= driver.findElement(By.xpath("//*[contains(text(),'Industry')]//following::dd[1]")).getText();

要获得成立的文本:

String Founded= driver.findElement(By.xpath("//*[contains(text(),'Founded')]//following::dd[1]")).getText();

所以您只需将 String 替换为所需的文本,如下所示

xpath = //*[contains(text(),'String')]//following::dd[1]

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    相关资源
    最近更新 更多