1

单击前一个 html 元素后,我试图访问网站WebElement上的a 。当您加载网站时,您会看到一个对象网格(对于这个问题的上下文,我们称它们为卡片)。如果您在单击任何内容之前检查页面,您会看到在div 下,有多个属于 class的 div 。正如您在下面的代码中看到的,我使用选择器获得了属于该类的第一个卡片对象。然后我调用该方法来模拟点击并等待几秒钟。在那之后,我遇到了麻烦。我想访问位于div 标签内的所选卡片的属性。经过一番搜索,我发现body > div.view > section.list.giitem.cardfirstCard.click()base"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对我来说看起来不错。关于为什么找不到元素的任何想法?

我的类.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"));
        }

    }

PS: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;
        }
    }
4

1 回答 1

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

}

在我的控制台上输出:

在此处输入图像描述


使用 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"));
}
于 2019-10-14T20:28:36.223 回答