使用WebDriver

时间:2019-10-14 17:26:02

标签: java selenium selenium-webdriver css-selectors

在单击上一个html元素后,我试图访问website上的WebElement。当您加载网站时,您会看到一个对象网格(在这个问题的上下文中,我们称其为卡片)。如果在单击任何内容之前检查页面,您会发现在body > div.view > section.list.gi div下,有多个div属于类item.card。如下面的代码所示,我使用firstCard选择器获得了属于该类的第一个卡片对象。然后,我调用.click()方法来模拟点击并等待几秒钟。在那之后,是我遇到麻烦的地方。我想访问位于base div标签内的所选卡的"body > div.overlay > div#cards"+base + " > div.one.card"属性。经过一番搜索,我发现id内每个卡对象的div.overlay(在用户首次加载网站时单击卡之后)是字符串cards +您在上方看到的base字符串。现在base字符串是每个卡对象的属性(在网站首次加载时单击它之前),因此我检索了该值并将其存储在String base字段中。因此,当我使用dataSelector字段单击开头的图标后尝试访问第一张卡片的“基本”属性时,我收到一个错误,指出该元素不存在,但dataSelector的值为body > div.overlay > div#cards1018031 > div.one.card,对我来说不错。关于为什么找不到元素的任何想法?

myClass.java:

    public static void main(String[] args) throws InterruptedException {

        String url = "https://dbz.space/cards/"; // The website to read data from

        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setJavascriptEnabled(true);
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"C:/Users/Steli/OneDrive/Documents/PhantomJS/phantomjs-2.1.1-windows/bin/phantomjs.exe");

        WebDriver driver = new PhantomJSDriver(caps);
        driver.get(url); // Connect to the url
        //WebElement button = driver.findElement(new By.ByCssSelector("body > div.view > section.more > div.content > div.btn.mat")); // Get the Show more Button element

        String firstCard = "body > div.view > section.list.gi > div.item.card";
        WebElement card = driver.findElement(By.cssSelector(firstCard));
        String base = card.getAttribute("base");

        if(isClickable(card,driver)) {
            card.click();
            Thread.sleep(5000);
            String dataSelector = "body > div.overlay > div#cards"+base + " > div.one.card";// + " > div.one.card > div.common > div.stats > div > div.stat";
            System.out.println(dataSelector);
            WebElement data = driver.findElement(By.cssSelector(dataSelector));
            System.out.println(data.getAttribute("base"));
        }

    }

P.S:isClickable()方法的实现如下:

private static boolean isClickable(WebElement el, WebDriver driver) {
        try{
            WebDriverWait wait = new WebDriverWait(driver, 6);
            wait.until(ExpectedConditions.elementToBeClickable(el));
            //System.out.println("clickable!");
            return true;
        }
        catch (Exception e){
            return false;
        }
    }

1 个答案:

答案 0 :(得分:0)

我刚刚复制了您的代码并粘贴到其中以使用chrome驱动程序进行检查并正常工作。但是,您可以使用显式等待来代替睡眠。 WebDriverWait

 public static void main(String[] args) throws InterruptedException {
            // TODO Auto-generated method stub

            System.setProperty("webdriver.chrome.driver", "D:\\Software\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("https://dbz.space/cards/"); 
            String firstCard = "body > div.view > section.list.gi > div.item.card";
            WebElement card = driver.findElement(By.cssSelector(firstCard));
            String base = card.getAttribute("base");
            card.click();
            Thread.sleep(5000);
            String dataSelector = "body > div.overlay > div#cards"+base + " > div.one.card";// + " > div.one.card > div.common > div.stats > div > div.stat";
            System.out.println(dataSelector);
            WebElement data = driver.findElement(By.cssSelector(dataSelector));
            System.out.println(data.getAttribute("base"));

}

在我的控制台上输出:

enter image description here


使用WebDriverWait代替睡眠。

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "D:\\Software\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://dbz.space/cards/"); 
        String firstCard = "body > div.view > section.list.gi > div.item.card";
        WebDriverWait wait = new WebDriverWait(driver, 20); 
        WebElement card=wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(firstCard)));
        String base = card.getAttribute("base");
        card.click();

        String dataSelector = "body > div.overlay > div#cards"+base + " > div.one.card";
        System.out.println(dataSelector);
        WebElement data =wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(dataSelector)));
        System.out.println(data.getAttribute("base"));
}