How to locate the element as per the given HTML - selenium

<input value="Order (ESHOP)" class="btn" name="order_eshop" title="Order (ESHOP)" type="button" onclick="Sfdc.logServer('CUSTOM_URL_BUTTON', {id: '00b41000002iuj6', name: 'Order_ESHOP'}, Sfdc.Logging.LogLevel.INFO);
openIntegration('/servlet/servlet.Integration?scontrolCaching=1&lid=00b41000002iuj6&eid=a0863000007SV1q&ic=1&isdtp=vw&linkToken=VmpFPSxNakF4T1MweE1TMHhNRlF5TVRvd09EbzBNaTQyT1RWYSx0eno3X1lSS1lOY1hORmtKb2ZvM3BxLFlXWmtNR0po', 'height=600,location=no,resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=1', 1)">
I' am not able to find the xpath for this. I have same element on a single path Order (ESHOP). When I'am trying to use indexing it is not finding the correct xpath.

The desired element is a JavaScript enabled element so to locate/interact with the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.btn[name='order_eshop'][value='Order (ESHOP)'][title='Order (ESHOP)']")));
Using XPATH:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='btn' and #name='order_eshop'][#value='Order (ESHOP)' and #title='Order (ESHOP)']")));

Related

How to locate the button element using Selenium?

I need to find the xpath of the help button for my automation tests.
Here is the HTML element:
<button data-component-id="nokia-react-components-iconbutton" tabindex="0" class="ActionComponent__ActionComponentDiv-sc-st3635-0 iPWdbh IconButton__StyledButton-sc-1dtic80-0 biWRLS NSPAppBanner__HelpButton-sc-761pc0-3 dvtRqt" mode="dark" type="button" xpath="1"></button>
<svg class="HelpOutline__NSPSvgIcon-sc-7y3t1-0 geSzXs HelpWidget__StyledHelp-sc-1fdz44x-0 cCTbqa" viewBox="0 0 24 24" xpath="1">
</svg>
I tried with this XPath, but it didn't work:
//*[#data-component-id="nokia-react-components-iconbutton"]//svg[contains(#class,"HelpOutline")]
If you want to select the <button> element before the <svg>, try the following XPath:
//*[#data-component-id="nokia-react-components-iconbutton" and following-sibling::svg[1][contains(#class,"HelpOutline")]]
The <button> element itself should be clickable and both the locator strategies should work:
css_selector:
button[class*='HelpButton'][data-component-id='nokia-react-components-iconbutton']
xpath:
//button[#data-component-id='nokia-react-components-iconbutton' and contains(#class, 'HelpButton')]
However, as the element is a React element so to 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:
Using Java and xpath:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#data-component-id='nokia-react-components-iconbutton' and contains(#class, 'HelpButton')]"))).click();
Using Python and css_selector:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='HelpButton'][data-component-id='nokia-react-components-iconbutton']"))).click()
Note: For Python clients you have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try this XPath to locate button:
'//*[name()="svg" and contains(#class,"HelpOutline")]/preceding-sibling::button'
Note that svg tag is not a part of standard HTML-namespace, so //svg won't work. You need to use //*[name()="svg"] instead

Selenium always gives org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:

The text "Congratulations!" apprears on the site only when the transaction is successful.
I am trying to capture the text of this element using JavascriptExecutor as the type is set to hidden but selenium always displays:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='form-header']//div[contains(text(),'Congratulations')]"}
(Session info: chrome=85.0.4183.121)
Code 1 (Not Working)
WebElement ele = driver.findElement(By.xpath(//div[#class='form-header']//div[contains(text(),'Congratulations')]));
JavascriptExecutor js = (JavascriptExecutor)driver;
String text = (String)js.executeScript("return arguments[0].value",ele);
System.out.println(text);
Code 2 (Not Working)
WebElement ele = driver.findElement(By.xpath(//div[#class='form-header']//div[contains(text(),'Congratulations')]));
JavascriptExecutor js = (JavascriptExecutor)driver;
String text = (String)js.executeScript("return arguments[0].innerHTML",ele);
System.out.println(text);
The HTML code for this part is as follows:
<input id="hdnWindowLocationHost" name="hdnWindowLocationHost" type="hidden" value="/" data-value="themes/custom">
<div id="sgw" class="sgw SGW-header">
</div>
<div class="page">
<div class="content">
<div class="form">
<div class="form-header">
<div class="headline">Congratulations!</div>
Your password has been reset. Please log in below with your Username and password.
</div>
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:
Using cssSelector:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.form-header > div"))).getText());
Using xpath and text():
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='form-header']/div[text()='Congratulations!']"))).getText());
Using xpath and contains():
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='form-header']/div[contains(., 'Congratulations')]"))).getText());

How to locate the element as per the HTML provided?

<a class="action showcart active" data-bind="scope: 'minicart_content'" href="http://52.14.171.179/xstpl/checkout/cart/">
I have tried this one but unable to proceed -
driver.findElement(By.linkText("cart")).click();
As per the HTML you have provided to invoke click() the element you have to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.action.showcart.active[href='http://52.14.171.179/xstpl/checkout/cart/']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='action showcart active' and #href='http://52.14.171.179/xstpl/checkout/cart/']"))).click();
findElement(By.partialLinkText("cart"))
And make sure that the string "cart" is case sensitive

Selenium driver find element not working

Selenium web driver finds element unable to find input field on the web page I tried this each and every option XPath, by CSS, by name. the testing site on the local environment.
HTML:
<input class="form-control ng-pristine ng-untouched ng-valid ng-empty" ng-model="email" placeholder="Username" aria-describedby="username" type="text">
xpath:
/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input
css selector:
html.no-js.ng-scope body.pace-done.full-width div#wrapper
div.page-wrapper.white-bg.ng-scope
div.wrapper.wrapper-content.ng-scope div.login-bg.ng-scope
div.container div.row
div.col-sm-6.col-sm-offset-3.col-md-4.col-md-offset-4 div.login-form
form.ng-pristine.ng-valid div.col-sm-12.plr10px div.form-group
div.input-group
input.form-control.ng-pristine.ng-untouched.ng-valid.ng-empty
driver.findElement(By.name("username"))
Here is the Answer to your Question:
As per the HTML you provided, you can use the following xpath to identify the element-
WebElement element = driver.findElement(By.xpath("//input[#ng-model='email' and #placeholder='Username']"));
Incase you are facing an ElementNotVisible exception you can induce ExplicitWait to wait for the element to be clickable as follows:
WebDriverWait wait7 = new WebDriverWait(driver, 10);
WebElement element7 = wait7.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#ng-model='email' and #placeholder='Username']")));
element7.click();
Let me know if this Answers your Question.
Try this, this should work.
WebElement emailInput = new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[ng-model='email']")));
JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement Username = driver.findElement(By.xpath("//*[#placeholder='Username' and #type='text'"]));
je.executeScript("arguments[0].scrollIntoView(true);",Username);
Username.sendKeys("Name");

I would like to click on New Contact button from web page for my selenium script

I would like to click on a "New Contact" button for my selenium script. I have tried:
driver.findElement(By.id("btn-group.contact_list-menu-contact_add")).click();
And by xpath as well, but it is not working. How could I get this working?
<div class="btn-group left">
<a id="contact_list-menu-contact_add" class="Button btn-contactadd primary SaveItem" href="javascript:">New Contact</a>
</div>
You are searching by an incorrect id value, use contact_list-menu-contact_add instead:
driver.findElement(By.id("contact_list-menu-contact_add")).click();
Or, by a CSS selector:
driver.findElement(By.cssSelector(".btn-group .btn-contactadd")).click();
driver.findElement(By.cssSelector(".btn-group #contact_list-menu-contact_add")).click();
driver.findElement(By.cssSelector("#contact_list-menu-contact_add")).click();
Or, by a link text:
driver.findElement(By.linkText("New Contact")).click();
If the target element is inside an iframe, you would need to switch into the context of the frame before searching for the element. Assuming that your frame has contactURL id, this is how to switch to it:
driver.switchTo().frame("contactURL");
If you are getting NoSuchElementException as you have mentioned in the comment, There are may be two reason :-
May be when you are going to find element, it would not be present on the DOM, So you should implement WebDriverWait to wait until element visible and clickable as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.id("contact_list-menu-contact_add")));
el.click();
May be this element is inside any frame or iframe. If it is, you need to switch that frame or iframe before finding the element as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("contactURL"));
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.id("contact_list-menu-contact_add")));
el.click();
Edited :- As I see from your provided HTML this button is inside <div id="btn-new-group" class="btn-group-actions left" style="display: none;"> which is set to be invisible, that's why you are not able to find button. You should make it visible first then try to find as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("contactURL"));
WebElement invisibleDiv = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("btn-new-group")));
//Now make it visible first
((JavascriptExecutor)driver).executeScript("arguments[0].style.display = 'block';", invisibleDiv);
//Now find contact button
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.id("contact_list-menu-contact_add")));
el.click();
Hope it helps...:)