Selenium can't locate ANY element - java

So I have a webscraper which first of all needs to get past the cookie banner of a given website. Normally I'd just locate the element by id or classname and be done with it, but on this site none of the elements can be located. I've tried/checked the following:
The element of interest is <div id="cookiescript_accept" tabindex="0" role="button" data-cs-i18n-text="[]">Alles accepteren</div>
The element is not part of an iframe
The element is not part of a shadow DOM
Using wait.until(ExpectedConditions.visibilityOfElementLocated hits the 15 seconds timeout
Using driver.executeScript("return document.getElementById('cookiescript_accept');"); doesn't work either
The parent element and parent of parent can also not be found
I'm still fairly new to Selenium and HTML so I must be missing something, please tell me if you know what that is
Code:
public void loadUrl(String url) {
System.out.println("\t\t- loadUrl " + url);
idle5000();
driver.get(url);
idle5000();
setWindowSize();
idle5000();
printFirefoxCPU();
scrollViewport();
idle5000();
printFirefoxCPU();
}
loadUrl("https://www.schoolplaten.com/");
prepRunnable.getDriver().findElement(By.id("cc-cookiescript_accept")).click();
// -> NoSuchElementException
WebDriverWait wait = new WebDriverWait(prepRunnable.getDriver(), 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("cookiescript_accept")));
// -> Timeout
WebElement elem = (WebElement) prepRunnable.getDriver().executeScript("return document.getElementById('cookiescript_accept');");
elem.click();
// -> elem is null

I could locate the web element with the below XPath
//*[name()='div' and #id='cookiescript_accept']
and could perform click on it with the help of below code :
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[name()='div' and #id='cookiescript_accept']"))).click();

To click() on the element ALLES ACCEPTEREN you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("div#cookiescript_accept")).click();
xpath:
driver.findElement(By.xpath("//div[#id='cookiescript_accept']")).click();
However, the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#cookiescript_accept"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='cookiescript_accept']"))).click();
Alternative
As an alternative you can also use the executeScript() as follows:
cssSelector:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#cookiescript_accept"))));
xpath:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='cookiescript_accept']"))));
Reference
You can find a detailed discussion on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element

Related

I am having problems identifying the element using selenium in java

I am having problems trying to identify the element using selenium and java...for the following link:
<a id="newSearchNavigateHeadingEventId_0" class="search-heading" ng-href="book/event?eid=757231&" target="_self" href="book/event?eid=757231&">
<h2 ng-bind="event.EventDisplayName" class="ng-binding">Makerspace Docklands - Safety Induction</h2>
</a>
I have tried the following ...
WebElement we = myDriver. findElement(By.linkText("Makerspace Docklands - Safety Induction"));
WebElement we = myDriver.findElement(By.id("newSearchNavigateHeadingEventId_0"));
WebElement we = myDriver.findElement(By.xpath( " //a[#id='newSearchNavigateHeadingEventId_01'] "));
WebElement we = myDriver.findElement(By.xpath("//a[text()='Makerspace Docklands - Safety Induction']"));
WebElement we = myDriver.findElement(By.xpath("//a[#href='book/event?eid=757231&']")) ;
But I keep getting the following message...
org.openqa.selenium.NoSuchElementException: Unable to locate element: ....
Could some-one suggest what would be the correct path so that I can click on the link with:
we.click();
To identify the element with text as Makerspace Docklands - Safety Induction you can use either of the following Locator Strategies:
cssSelector:
WebElement we = driver.findElement(By.cssSelector("a.search-heading[id^='newSearchNavigateHeadingEventId']>h2.ng-binding[ng-bind*=EventDisplayName]"));
xpath:
WebElement we = driver.findElement(By.xpath("//a[#class='search-heading' and starts-with(#id, 'newSearchNavigateHeadingEventId')]/h2[contains(., 'Makerspace Docklands') and contains(#ng-bind, 'EventDisplayName')]"));
However, as the element is a dynamic element so to identify the element you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
cssSelector:
WebElement we = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.search-heading[id^='newSearchNavigateHeadingEventId']>h2.ng-binding[ng-bind*=EventDisplayName]")));
xpath:
WebElement we = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#class='search-heading' and starts-with(#id, 'newSearchNavigateHeadingEventId')]/h2[contains(., 'Makerspace Docklands - Safety Induction') and contains(#ng-bind, 'EventDisplayName')]")));
Reference
You can find a detailed discussion on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element
Thank you...
I found this also worked for me...
WebElement we = myDriver.findElement (By.xpath ("//*[contains(text(),'Makerspace Docklands - Safety Induction')]"));
we.click();

Unable to locate element: {"method":"xpath","selector":"//li[#id="tablist1-tab3"]"} error using Selenium and Java

I have received this error for several times:
Unable to locate element: {"method":"xpath","selector":"//li[#id="tablist1-tab3"]"}
Code that I have used is:
options.addArguments("--headless");
options.addArguments("window-size=1200x900");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement tab = driver.findElement(By.xpath("//li[#id=\"tablist1-tab3\"]"));
tab.click();
Can someone help me with this error?
You need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li#tablist1-tab3"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#id=\"tablist1-tab3\"]"))).click();
WebElement tab = driver.findElement(By.xpath('//li[#id="tablist1-tab3"]'));
try this
Make sure the element is not inside an iFrame. If it is, then you need to first find the iFrame element and use Selenium's switchTo(). After that you will be able to locate elements inside the iframe.

Unable to find the element in a Salesforce search results

I have searched for an Account in Salesforce and it gives a few search results. But I'm unable to find the element in Selenium.
I have tried using absolute/relative xpath and CSSSelector, LinkText as well.
Used: driver.findElement(By.linkText("ILT_Order1")).click();
ILT_Order1
I am unable to find the element.
Induce WebDriverWait and elementToBeClickable and following xpath.
If still not found then check if there any iframe available on the page.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-seclke='Account'][text()='ILT_Order1']")));
element.click()
As the element is a JavaScript enabled element so to click() on the element you need to use elementToBeClickable() and you can use either of the following Locator Strategies:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("ILT_Order1"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[data-seclke='Account'][onmousedown^='searchResultClick'][href]"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-seclke='Account' and text()='ILT_Order1'][starts-with(#onmousedown, 'searchResultClick')]"))).click();

How to locate the element using selenium-webdriver and Java

How can I call xpath for class?
<button class="inline" data-ember-action="" data-ember-action-11310="11310">Load</button>
I tried to to call it by :
By.xpath("//span/button[text()='Load'][1]");
but it is not able to locate element.
Use this xpath:
//button[#class='inline' and text()='Load']
The element seems to be a Ember.js enabled element so to click() on the element you have to induce WebDriverWait for the elementToBeClickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.inline[data-ember-action]"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='inline' and text()='Load'][#data-ember-action]"))).click();

Unable to locate the element as per the below anchor tag

Please advise how to locate the link for New business tag in the below code. I have tried the following xpath but it didnt work:
driver.findElement(By.linkText("NEW BUSINESS")).click
driver.findElement(By.xpath("//span[#class='hdBottomBar']/a[1]"))
HTML:
<span class="hdBottomBar">
<a class="hdTopBar" href="javascript: void navCntl('NewBusiness','NavBar');" onmouseover="window.status='New Business';return true" onmouseout="window.status='';return true" name="newBusiness">NEW BUSINESS</a>
The element is a JavaScript enabled element so to invoke click() you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("NEW BUSINESS"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.hdBottomBar>a.hdTopBar[name='newBusiness']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='hdBottomBar']/a[#class='hdTopBar' and #name='newBusiness'][text()='NEW BUSINESS']"))).click();
try with:
//span[#class='hdBottomBar']/a[#name='newBusiness']
or
//span[#class='hdBottomBar']/a[text()='NEW BUSINESS']

Categories

Resources