Reputation: 11
Could you, please, help me? I can't figure out why does:
"Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element"
keeps showing up.
I try to click on "Accept all" pop up which is in the red circle in the pic. I copied the selector where the red arrow points out. Below is my entire code.
I really dont know is it something with the code or did I copy something wrong from the devtool?
I try to do everything from this tutorial:
https://www.youtube.com/watch?v=QriNzMr1FeE&list=PL954C7AD3A221748D&index=94&t=1736s&ab_channel=edureka%21 only with a different website.
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);
I would just love to know how to close this type of pop ups. Unfortunately nothing works for me.
Upvotes: 1
Views: 122
Reputation: 1778
There is a iframe that needs to be switched to:
<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>
Code sample:
see comments as well
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();
}
}
Output:
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
Upvotes: 1