I am writing code in Javascript to automate the website https://www.fashionette.co.uk/. I have to use cookies when the user lands on the page for the first time
I am writing the following code to get cookies:
JavascriptExecutor js = (JavascriptExecutor)webdriver; WebElement element= (WebElement) js.executeScript("return document.getElementById('usercentrics-root').shadowRoot.querySelector("[data-testid='uc-accept-all-button']");"); element.click()
it locate element in browser correctly:
But In Intellij it gives me error:
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebElement.click()" because "element" is null
Advertisement
Answer
If you are using Java
create a browser instance and find elements:
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("http://www.google.com/"); WebElement WE = driver.findElementByCss("YourCSSSelector"); WE.click();
If you are using JavaScript
change your into:
WebElement element = (WebElement) js.executeScript ("document.getElementById('usercentrics-root'). shadowRoot.querySelector("[data-testid='uc-accept-all-button'])");