Unable to find the element in a Salesforce search results - java

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

Related

Selenium can't locate ANY element

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

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.

Find Xpath for element in Selenium Java

I'm trying to find a element in selenium with this XPATH. I get this in firefox web browser.
/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/div[1]/a
HTML code
<br><h3 class="LC20lb DKV0Md">Gmail by Google</h3><div class="TbwUpd NJjxre"><cite class="iUh30 bc tjvcx">www.google.com<span class="eipWBe"> › gmail</span></cite></div>
My Selenium Code
driver.findElement(By.xpath("/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/div[1]/a")).click();
driver.findElement(By.linkText("Sign in")).click();
But it's not working. Help me.
As the element is an Angular element to click on it, 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("a[#href='https://www.google.com/gmail/'] h3"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[text()='Gmail by Google']"))).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