eclipse 在线程“main”中出现异常,无此元素

w51jfk4q  于 2022-11-29  发布在  Eclipse
关注(0)|答案(1)|浏览(225)

你能帮帮我吗?我不知道为什么:

"Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element"

不断出现。
我试着点击“接受所有”弹出窗口,这是在红色圆圈的图片。我复制了选择器的红色箭头指出。下面是我的整个代码。
我真的不知道是代码的问题还是我从devtool复制了一些错误?
我尝试做的一切从这个教程:
https://www.youtube.com/watch?v=QriNzMr1FeE&list=PL954C7AD3A221748D&index=94&t=1736s&ab_channel=edureka%21仅与不同的网站。

package nauka.selenium;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class launchbrowser {
    
    public static WebDriver driver = null;
    public static void main(String[] args) throws InterruptedException
    
    {
        System.setProperty("webdriver.chrome.driver","D:\\gry\\selenium nauka\\nauka\\driver\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        
        driver.navigate().to("https://jbzd.com.pl");
        driver.manage().window().maximize();
        String title = driver.getTitle();
        
        if(title.equalsIgnoreCase("jbzd.com.pl"))
            System.out.println("Title matches");
        else
            System.out.println(title);
        
        String tagname=" ";
        tagname = driver.findElement(By.cssSelector("#oa-360-1669467379737_1u9qm5gr5 > div > div > div > div > div > div.ZeroLayer__dark___w7kyt8.ZeroLayer__zeroLayer___2R20B_ > div:nth-child(2) > button > span.MuiButton-label")).getText();
        System.out.println(tagname);

我只是想知道如何关闭这种类型的弹出窗口。不幸的是没有为我工作。

8oomwypt

8oomwypt1#

有一个iframe需要切换以:

<iframe allowtransparency="true" frameborder="0" scrolling="0" marginheight="0" topmargin="0" id="cmp-iframe" style="width: 1920px; height: 331px; margin: 0px; padding: 0px; position: fixed; top: 0px; left: 0px; display: block; z-index: 2147483647;"></iframe>

代码示例:

也可参见评论

package tests;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Pegenon {

    public static String chromedriverPath = System.getProperty("user.dir") + "\\resources\\chromedriver.exe";   

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", chromedriverPath);
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--ignore-certificate-errors");
        options.addArguments("--start-maximized"); // instead of driver.manage().window().maximize();
        options.addArguments("--disable-notifications");
        WebDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        driver.get("https://jbzd.com.pl"); // same as navigate().to
        String title = driver.getTitle(); // title != url, usually different
        if (title.equalsIgnoreCase("jbzd.com.pl")) {
            System.out.println("Title matches");
        }
        else {
            System.out.println(title);
        }

        driver.switchTo().frame("cmp-iframe"); // id or name of the iframe
        // locating the button using dynamic xpath instead of the ugly css selector
        WebElement acceptAllBtn = driver.findElement(By.xpath("//button[@data-button-type='acceptAll']"));
        System.out.println(acceptAllBtn.getText());
        driver.quit();
    }

}

输出:

Starting ChromeDriver 107.0.5304.62 (1eec40d3a5764881c92085aaee66d25075c159aa-refs/branch-heads/5304@{#942}) on port 2325
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Lis 28, 2022 2:12:35 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Jbzd.com.pl - najgorsze obrazki w internecie!
PŘIJMOUT VŠE

相关问题