how to find xpath in selenium webdriver - java

Following is the HTML of the page, How do i get the Xpath or is there anyother way to automate using java?In fact, we shud click on this "continue" button.
<regform-button>
<button ng-disabled="activityIndicator" ng-click="validate()" type="button">
<div template="api-loader" ng-http-loader="">
<div class="http-loader__wrapper ng-hide" ng-show="showLoader" ng-include="template">
<span class="api-loader"></span>
</div>
</div>
<ng-transclude>
</button>
</regform-button>

If you are using Google Chrome, right click on the button, and select Inspect in the popup. The html will open in a developer tools frame. Right click on the element in the developer tools frame, hover over copy, select copy xpath.
Here is the XPath to the form on the URL.
//*[#id="main-frame"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button
WebDriver driver = new ChromeDriver();
driver.get("https://uk.match.com/unlogged/landing/2016/06/02/hpv-belowthefold-3steps-geo-psc-bowling?klid=6740");
//fill in fields
WebElement element = driver.findElement(By.xpath("//*[#id=\"main-frame\"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button"));
element.click();

driver.findElement(By.xpath("//regform-button/button")).click();

Try opening the console for Google Chrome and typing the following:
document.evaluate("//regform-button/button", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()
It works...that's if you've specified the required fields. Also be sure to wait for a while (2 secs to be sure) after specifying the required fields as there might be an additional load after setting the required fields.
Edit: The code is javascript..I just took the xpath given by the previous answers and used that to locate the element through js. I'm saying that "//regform-button/button" should work..

Once you have selected the required values from the drop-downs appearing before Continue button, you can click on Continue button using the following statement:
driver.findElement(By.xpath("//span[text()='Continue']")).click();

The following xpath worked for me.But I dont know, why is it different for each different browser.
For Firefox :
`driver.findElement(By.xpath("//*[#id='main-frame']/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button")).click()`;
For chrome: driver.findElement(By.xpath("//regform-button/button")).click();

Related

Selenium | Unable to locate input element

No idea how to address this input text field element with selenium / Java (openjdk 11 2018-09-25).
I tried xpath, cssSelector etc. it never works. Its always "Unable to locate element".
<slot name="input">
<input part="value" tabindex="0" aria-labelledby="vaadin-text-field-input-3">
</slot>
This did NOT work:
driver.findElement(By.xpath("//input[#aria-labelledby='vaadin-text-field-input-3']")).sendKeys("test");
Is there a solution for this?
UPDATE:
This thread solved partly my problem. The solution that worked for me can be found here link.
To send a character sequence to the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
css_selector:
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(
By.cssSelector("div.vaadin-text-field-container div[part=input-field][id^='vaadin-text-field-input'] slot[name='input'] > input[part='value'][aria-labelledby^='vaadin-text-field-input']")
)).sendKeys("pixelhead");
xpath:
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(
By.xpath("//div[#class='vaadin-text-field-container']//div[#part='input-field' and starts-with(#id, 'vaadin-text-field-input')]//slot[#name='input']/input[#part='value' and starts-with(#aria-labelledby, 'vaadin-text-field-input')]")
)).sendKeys("pixelhead");
Xpath:
//div[#class='vaadin-text-field-container']//descendant::input[#part='value' and starts-with(#aria-labelledby, 'vaadin-text-field-input')]
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
If this is unique //div[#class='vaadin-text-field-container']//descendant::input[#part='value' and starts-with(#aria-labelledby, 'vaadin-text-field-input')] then you need to check for the below conditions as well.
Check if it's in any iframe/frame/frameset.
Solution: switch to iframe/frame/frameset first and then interact with this web element.
Check if it's in any shadow-root.
Solution: Use driver.execute_script('return document.querySelector to have returned a web element and then operates accordingly.
Make sure that the element is rendered properly before interacting with it. Put some hardcoded delay or Explicit wait and try again.
Solution: time.sleep(5) or
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='vaadin-text-field-container']//descendant::input[#part='value' and starts-with(#aria-labelledby, 'vaadin-text-field-input')]"))).send_keys("test")
If you have redirected to a new tab/ or new windows and you have not switched to that particular new tab/new window, otherwise you will likely get NoSuchElement exception.
Solution: switch to the relevant window/tab first.
If you have switched to an iframe and the new desired element is not in the same iframe context then first switch to default content and then interact with it.
Solution: switch to default content and then switch to respective iframe.
You can start debugging from step1.
Update:
Solution specific to the problem:
Thread.sleep(2000);
WebElement inputButton = (WebElement) ((JavascriptExecutor)driver).executeScript("return document.querySelector('#TextFieldTitle').shadowRoot.querySelector('#vaadin-text-field-input-3 > slot:nth-child(2) > input')");
inputButton.sendKeys("test");
in the place of paste query selector here you will have to go to dev tools again in goog chrome by pressing F12, and then
Go to that input box
Do a right click
Select copy
Select copy JS path.
Ctrl + v into notepad to see what you've got from the dev tool.
It'd be something like this:
document.querySelector("#vaadin-text-field-input-3 > slot:nth-child(2) > input")
replace this paste query selector here with the stuff that is wrapped inside ""
Per the html provided, this works:
driver.find_element(By.XPATH, "//input[#part='value' and contains(#aria-labelledby, 'vaadin-text-field-input')]"]
x = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#part='value' and contains(#aria-labelledby, 'vaadin-text-field-input')]")))
x.send_keys('This is typed here by selenium')
print(f"the typed text in input box is: {x.get_attribute('value')}")
Output:
the typed text in input box is: This is typed here by selenium
Process finished with exit code 0
Looks like part of HTML looks like:
<div class="vaadin-text-field-container">
<label part="label" id="vaadin-text-field-label-3"></label>
<div part="input-field" id="vaadin-text-field-input-3">
<slot name="prefix"></slot>
<slot name="input">
<input part="value" tabindex="0" aria-labelledby="vaadin-text-field-input-3">
</slot>
</div>
</div>
Much better is to build your XPATH locator from some id field:
//div[#id='vaadin-text-field-input-3']//input[contains(#aria-labelledby, 'vaadin-text-field-input')]"]
Also, as mentioned earlier have to check:
if it is unique for this page
if the input element is not dynamic
If you found solved that comment you could move to the code part and use the founded locator with WebDriver:
driver.findElement(
By.xpath("//div[#id='vaadin-text-field-input-3']//input[contains(#aria-labelledby, 'vaadin-text-field-input-3')]"]"
)).sendKeys("test");
Also, keep in mind that you have to know that element is already loaded on a page. As suggested before you could use some explicit wait for your element.

Selenium WebDriver "java", I can not click on button on footer

The scenario is:
Try to add experience in linkedin.
Then click on save button to save the added experience.
The below is html code for this button:
<button class="pe-form-footer__action--submit form-submit-action Sans-15px-white-100%" type="submit">
Save
</button>
I am trying to find it by xpath using:
#FindBy (xpath = "//*[contains(text(), 'Save')]")
WebElement saveExperienceButton;
Following screenshot may help:
I will appreciate your help.
if you do not mind css/xpath selectors that do not look very elegant, you can always open up Chrome developer tools on the website you wanna test with Selenium, mark the DOM elements you wanna access and in the context menu choose 'Copy xpath' or 'copy selector':
Try this xpath:
(//*[text()='Save'])[2]
On my profile there are 2 Save buttons - the second one is the skill save. Also, you might want to check this question for the contains syntax.
Creating an XPath using text is a less preferable way. instead of that use other attribute value which is unique.
for ex: in your case
//footer//*[contains(#class, 'form-submit')]

How to click on select options using selenium

I can't click on option element-
website: http://www.oferty.net
When I am trying to select one option in the drop-down list. Unfortunately
I receive the message:
"Cannot click on option element. Executing JavaScript click function
returned an unexpected error"
What did I do wrong with the following code:
Select estateType = new Select(driver.findElement(By.id("ps_type")));
estateType.selectByVisibleText("domy");
Please help.
I quicky check the HTML code of the dropdown you are trying to select and it's not a HTML dropdown. it's custom dropdpown created with CSS and so you can't use the default function provided by Selenium to select Dropdown Value.
<div class="jquery-selectbox-list jquery-custom-selectboxes-replaced-list" style="width: 113px; height: 9em; display: none;">
<span class="jquery-selectbox-item value-998 item-0">rynek pierwotny
</span>
...
</div>
Possible Solutions
Try SendKeys on jquery-custom-selectboxes-replaced-list element and see if it's working, if not
Click on jquery-custom-selectboxes-replaced-list and then scroll to the element you are interested in and click on it.
It's a simulated dropdown list, not native. Selenium Select Class can only work on native dropdown list define by Select Tag. Even there is also embed a native dropdown, but it's hidden, that's why report can't click on option error. And When you operate on the dropdown from page, the embed native dropdown is always hidden, thus actually you not operated on the embed native one.
Try below code:
public void choosePsType(String psType) {
// find the container node of the dropdown list
WebElement psTypeSelect = driver.findElement(By.cssSelector("label[for='ps_type'] + div"));
// click on the selector to expand options
psTypeSelect.findElement(By.cssSelector("span.jquery-selectbox-currentItem")).click();
// choose wanted option
String xpath = String.format(".//span[contains(#class, 'jquery-selectbox-item')][.='%s']", psType);
psTypeSelect.findElement(By.xpath(xpath)).click();
}

How to interact with dropdown having html tag as input instead of select in selenium webdriver

I've dropdown control in the web application having following html code
<input class="dynamic-list-widget-input ui-widget ui-widget-content" title="" autocomplete="off" aria-invalid="false">
I've tried accessing using selenium webdriver through xpath(relative/absolute), cssSelector but no to avail i got following exception
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[#id='With-Attachment']/div[2]/div[2]/div[5]/div[1]/span/input"}
Command duration or timeout: 10.12 seconds
It looks very likely you get the wrong xpath for that input element.
if you are using Chrome, you can do as follows:
right click on the element in the webpage.
click "inspect element" in pop up menu
right click the highlighted html code on the right, the then click "copy xpath"
you will get the xpath of that element and compare what it is in your code.

Button location error using Xpath?

I have to test a dynamic app using a button like that:
<button class="btn btn-primary btn-mini" ng-click="addAnswer(question)" ng-show="question.editing">Add Answer</button>
I was trying to locate button with that but I failed
getDriver().findElement(By.xpath("//img [#ng-click='addAnswer(question)']")).click();
using css
List AddAnswerBtn =
getDriver().findElements(By.className("btn-primary"));
AddAnswerBtn.get(0).click();
What is the correct line to locate it? I don't want to locate it using class and I wants to use Xpath to locate button location.
Your example shows it is a button, but your XPath is looking for an image:
//img[#ng-click='addAnswer(question)']
The above is what you are using, with img as the element type.
Change this to button.
Xpath doesn't work properly sometimes in chrome browser. Use firefox driver to find it, if you are not sure if your xpath is correct, use Firepath. Or in Chrome Xpath Helper.
Your xpath refers to a image, not button.
getDriver().findElement(By.xpath("//img [#ng-click='addAnswer(question)']")).click();
Change this to
getDriver().findElement(By.xpath("//button[#ng-click='addAnswer(question)']")).click();

Categories

Resources