How to find web element if it is changing in selenium - selenium-webdriver

How do I create a Xpath of any element in web page when it is changes each page load or refresh using selenium

If there is 1 submit button, you can find element by checking input type submit
//input[#type='submit']
If there are more and you know it place in page for example 2. Search by child number as:
( //input[#type='submit']) [2]

You can try this method it works fine.
Here is my xpath,
//*[#id="CaseTypes_39aa6635-cd44-4104-a9f7-e33c3f96e2ab__Amount"]
Here is my code:
[FindsBy(How = How.XPath, Using = "//*[starts-with(#id, 'CaseTypes_') and substring(#id, string-length(#id) - string-length('__Amount') +1) = '__Amount']")]
I hope it helps!!

Related

click method is not working properly in selenium webdriver

i am trying to Automate flipkart website in which i am trying to change address but "add new adress" is not getting clicked i have attached the snapshot
my code is like driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).click();
please give the appropriate help
I doesn't look that you are clicking active element, the xpath is //*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span not correct it clicks on some span.
Use Firepath https://addons.mozilla.org/en-US/firefox/addon/firepath/ to get the xpath.
To ensure that button is clickable Use isDisplayed() and isEnabled() method before clicking on "Add New Address" button, this method return boolean value.
driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).isDisplayed();
driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).isEnabled();
Also you can verify that element is exist on page or not using below code
if(driver.findElements(byVal).size()!=0){
// Element is present.
}
hope it may helpful to identify cause of issue, why its not clickable.
First and foremost, Use a customized Xpath instead of the one you are using which is extracted directly from the browser. If no customized Xpath can be constructed then try a customized Css or any other locator if possible.
Try to go through the following attempts in order (I hope you can grasp why this is):
1- If .click() still doesn’t work, then keep on changing the values of the attributes you are using to construct your customized xpath, cssSelector, or locator.
2- Use the Actions class.
3- Use JavaScript executioner
4- instead of click(), try using any of: .sendKeys(“\n”). Or .sendKeys(keys.ENTER) or .sendKeys(keys.RETURN)

Webdriver and push button

i try to make in webdriver a test.
go to http://programyedukacyjne.pl/konkurs/galeria-prac.html
click for example first vote "GŁOSUJ [star]"
and should appear a box to put email.
But nothing happend just add to url in browser "#vote_box_22"
I tryed use click(), Action and javascript click but still there no appear a form...
here is begine:
driver.get("http://programyedukacyjne.pl/konkurs/galeria-prac.html");
WebElement location = driver.findElement(By.name("filter_miejscowosc"));
location.sendKeys("nowy sącz", Keys.ENTER);
here is where I have problem
WebElement voteBtn = driver.findElement(By.xpath("html/body/div[4]/div/div[3]/div/div[4]/div/div[1]/div[2]/div[2]/div[1]/a"));
voteBtn.click();
my secound solution
new Actions(driver).moveToElement(voteBtn).click().perform();
Try using a more specific selector that pinpoints the link more accurately in a less brittle way. The following CSS selector should select the vote link for the first result shown in the list:
WebElement voteBtn = driver.findElement(By.cssSelector("div.praca:nth-of-type(1) a[href*='vote_box']"));
voteBtn.click();

Protractor Not select first Element from an autocomplete search address

i need to select the first element from an autocomplete search box ,
when autocomplete shows its hover other element,so other element not clickable by protractor ,
solution
element.all(by.css('[ng-model="address"]')).get(0) is not work for me but work in another computer ,
same script work in another computer ,
i checked protractor version , selenium version
i also try element.all(by.css('[ng-model="address"]')).first() ;
its also not work for me ,
do you have any idea how cant i get first element ?.
thanks
you can send enter key
protractor.Key.ENTER
yourelement.sendKeys('your text to send ', protractor.Key.ENTER);
The Autocomplete List is only visible if a curser is over the element.
this.selectFirstElement = function(element){
browser.sleep(3500);
browser.driver.actions().mouseMove(element);
element.sendKeys(protractor.Key.ARROW_DOWN);
element.sendKeys(protractor.Key.TAB);
};
Try adding a short delay (say 500ms) and then do click. Sometimes the rendering of autocomplete misses the first item.

how to click the span element in dropdown by selenium webdriver?

How to click smartphone element in dropdown by selenium Web driver?please find the code
<div id="tooltip_menu">
<li class="level0 nav-1 first level-top"
<a class="level-top" onclick="event1('Smart Phones')" href="http://www.vmall.my/index.php/smart-phones.html">
<span>Smart Phones</span>
Use an XPath expression, like this:
//span[text()='Smart Phones']
Set your expression to a By locator variable, say SmartPhoneSelectionLocator,
and then in your test, use SmartPhoneSelectionLocator.click();
So perhaps the code in your Page Object would look like:
By SmartPhoneSelectionLocator = By.xpath("//span[text()='Smart Phones']");
Also, it's recommended you actually wrap the behavior of the above action in a method in your Page Object.
Please inspect on selecting the value which frame has been loaded on webpage. You have to switch to that frame then select the value from dropdown.
driver.switchTo().frame(name_or_id);
now select the dropdown. hope it will work for you.

Cannot find button element on form using Webdriver

I'm having trouble trying to click on on a button within a form.
I've tried xpath, cssselector, className, id, but still cannot find it.
Here's the HTML snippet for the button:
<input type="button" value="Continue" id="ni-reg-btn-register" class="btnNext ni-reg-btn-register">
I'm using WebDriver in Java
Getting this trace:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 30.09 seconds
Frequency: 100%
Browser: Firefox
URL: https://subscription.thetimes.co.uk/webjourney/webj_capturecustomerdetails
I've tried each of the following lines of code one by one (but no luck):
driver.findElement(By.className("btnNext ni-reg-btn-register")).click();
driver.findElement(By.cssSelector("buttons#ni-reg-btn-register.btnNext ni-reg-btn-register")).click();
List<WebElement> buttonlist= driver.findElements(By.className("btnNext ni-reg-btn-register"));
driver.findElement(By.id("ni-reg-btn-register")).click();
driver.findElement(By.xpath("//*[#id="ni-reg-btn-register"]")).click();
I was able to click the Continue Button using the below xpath.
//input[#id='ni-reg-btn-register']
I would suggest trying a WebDriverWait call to wait for the element in question to exist on the page prior to interacting with it. The Java documentation can be found here. It appears that while you are using the correct locator and your page is working, there is a timing issue where Selenium is trying to access the element prior to it being loaded.
EDIT: I was unable to locate an element on the provided page with the supplied id. I assume I am missing a step, but I did a search on the HTML for that page and found nothing.
I dont find the above button on the page link you provided ..
better chk the page link and try to give some wait statement after page load ...
implicit wait would be better or simpli Thread.sleep(2000);

Resources