How to use driver.findElement to locate an element with below XML? - java

I have XML as below:
<input name="_jpfcpncuivr___ns535482039__j_id__ctru0:fragmentRegionStatic:0:it1" maxlength="6" style="text-transform:uppercase" type="text" class="af_inputText_content" id="_jpfcpncuivr___ns535482039__j_id__ctru0:fragmentRegionStatic:0:it1::content">
I have tried driver.findElement by id, name, className, cssSelector, or xpath but all failed and got an error:
no such element: Unable to locate element:

The desired element is a dynamic element so to locate and click() on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[class$='inputText_content'][id*='fragmentRegionStatic']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#class, 'inputText_content') and contains(#id, 'fragmentRegionStatic')]"))).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

How to get following text name using xpath?

I try with following xpath
driver.findElement(By.xpath("//div[contains(#class,'ReactTags__selected')]//span[1]/text()"));
but get following error
"org.openqa.selenium.InvalidSelectorException: invalid selector: The result of the xpath expression "//div[contains(#class,'ReactTags__selected')]//span[2]/text()" is: [object Text]. It should be an element."
<div class="ReactTags__selected">
<span class="tag-wrapper ReactTags__tag" style="opacity: 1; cursor: auto;" draggable="true">
"TestName"
<a class="ReactTags__remove">×</a>
</span>
</div>
Please try the below Xpath.
Hope it helps
//div[contains(#class,'ReactTags__selected')]//span[2]/a/text()
text() in the xpath returns text node, Selenium doesn't support it. Locate the element and use getText() to get the text
WebElement element = driver.findElement(By.xpath("//div[contains(#class,'ReactTags__selected')]//span[1]"));
String text = element.getText();
As the parent element contains the attribute draggable="true" invariably it's a dynamic element, precisely React element. Additionally as the text TestName is a Text Node you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
cssSelector:
System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].firstChild.textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.ReactTags__selected > span.tag-wrapper.ReactTags__tag")))).toString());
xpath:
System.out.println((String)((JavaScriptExecutor)driver).executeScript("return arguments[0].firstChild.textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='ReactTags__selected']/span[#class='tag-wrapper ReactTags__tag']")))));

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