I am trying to do SignIn for this. In it click on 'SignIn' which I have done it successfully. Now when trying to type in Username/Password using Xpath it shows exception which says
Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: element not visible
code is :-
driver.findElement(By.xpath(".//*[#id='load_form']/fieldset[1]/input")).sendKeys("test123");
driver.findElement(By.xpath(".//*[#id='load_form']/fieldset[2]/input")).sendKeys("test123");
I know this Xpath is same as used in SignUp form, so what are the other ways to locate these two fields? How we can use it by using cssselector?
Thanks for the help in advance.
Go One Level up on finding the relative xpath with
//div[#id='login']/form[#id='load_form']//input[#name='username']
//div[#id='login']/form[#id='load_form']//input[#name='password']
Try this
//For Username
driver.findElement(By.xpath("(//input[#name='username'])[2]")).sendKeys("username test");
//or
driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='usernam']")).click();
//For password
driver.findElement(By.xpath("(//input[#name='password'])[2]")).sendKeys("password test");
//or
driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='password']")).click();
the above codes are working for me.
There are basically two elements found by provided xpath, and it works with first found element which is invisible as your exception saying, you should try using cssSelector as below :-
driver.findElement(By.cssSelector("div#login input[name = 'username']")).sendKeys("test123");
driver.findElement(By.cssSelector("div#login input[name = 'password']")).sendKeys("test123");
Note:- For learning about cssSelector follow this url and for xPath follow this url
I'm suggesting you before selenium execution with the locator you need to sure that using locator is correct and returns single or multiple element at your browser console by pressing f12. for verify xPath you should use $x("your xpath expression") and for cssSelector you should use document.querySelector("your css selector expression")..
Hope it will help you..:)
Related
In my webdriver test I have a web page with a tag named c-wiz with the class="boqChromeogbviewView_ boqChromeapiViewView_ modeChromeScrollable_". I need to send an ESC key press to this element since it has a listener for it. So I tried
driver.findElement(By.xpath("//c-
wiz[#class='boqChromeogbviewView_']")).sendKeys(Keys.ESCAPE);
but it fails saying
Unable to locate element: {"method":"xpath","selector":"//c-wiz[#class='boqChromeogbviewView_']"}
Can someone please help?
With xpath you have to use whole class value or use contains like below:
driver.findElement(By.xpath("//c-
wiz[#class='boqChromeogbviewView_ boqChromeapiViewView_ modeChromeScrollable_']")).sendKeys(Keys.ESCAPE);
driver.findElement(By.xpath("//c-
wiz[contains(#class,'boqChromeogbviewView_']")).sendKeys(Keys.ESCAPE);
Use css selector instead:
driver.findElement(By.cssSelector("c-
wiz.boqChromeogbviewView_")).sendKeys(Keys.ESCAPE);
I am trying to locate those buttons as shown here:
And this is HTML for the same :
I tried with all possible ways but nothing works.
As per the HTML you have shared you can locate the Continue with New button with either the following options :
XPATH
//button[#class='slm-btn slm-btn-AdvanceFlow' and #id='Continue']/span[#class='slm-btn-text']
CSS_SELECTOR
button.slm-btn.slm-btn-AdvanceFlow#Continue > span.slm-btn-text
First, you have to try with id because it is the best way to get it because it is unique.
Otherwise, you can use XPath:
//button[#id='ContinueNew']
//button[contains(#id,'ContinueNew')]
I am new to WebDriver,and currently trying to write the code to click the button. The locator is not available so I have used Xpath,but it is not working as it should be. Kindly help me on this.
Button tag:
<button onclick="myFunction()">Try it</button>
My web drive code:
drive_url.findElement(By.xpath("html/body/button")).click();
Did you check your xpath in browser console. You can check xpath by writing $x("<your xpath>") in console. Try using "//button" instead of what you're using now.
You should be little careful writng the selector as well. Try to avoid flaky selector and make is as unique as possible.
By xpath = By.xpath("//button[contains(text(),'Try it')]");
drive_url.findElement(xpath ).click();
The above selector finds the button tag explicitly using text based search.
Try this:
WebElement btn = driver.findElement(By.tagName("button"));
String btnText= driver.findElement(By.tagName("button")).getText();
if(btnText.equals("Try it")){
btn.click;
}
I am new to Selenium. Not sure how to handle this scenario. I am working on a website which has several buttons with following code,
<a class="Some big class name" datacommunication="SelectItem" token="some token number" model-id="Id1" element="button">
<i class="classname">Book Ticket</i>
</a>
<a class="Some big class name" datacommunication="SelectItem" token="some token number" model-id="Id2" element="button">
<i class="classname">Book Ticket</i>
</a>
I tried to click on it using following commands,
ele = driver.FindElement(By.ClassName("Some big class name")); but it fails with following message, Compound class names are not supported. Consider searching for one class name and filtering the results.
ele = driver.FindElement(By.CssSelector("a[model-id='Id1']")); fails with 'Test method TestBot.HomeTest.bookTicket threw exception:
OpenQA.Selenium.WebDriverTimeoutException: Timed out after 10 seconds'
Tried using XPATH,
ele = driver.FindElement(By.XPath("\\\a[#model-id='Id1']")); doesn't work either.
I have no control on html. Can't change it.
Please let me know how to identify elements in such scenarios.
You can't have spaces in class names. Those are actually multiple classes separated by a space. You can find the above elements using a css selector
var ele = driver.FindElements(By.CssSelector(".Some.big.class.name"))
Of course, this will find both elements. To find just the first, you could use
var ele = driver.FindElement(By.CssSelector("a[model-id='Id1']"))
You can find help on css selectors here: http://www.w3schools.com/cssref/css_selectors.asp
Update:
I just noticed your XPath appears to have the slashes the wrong way around. If you wish to use XPath, try
//a[#model-id='Id1']
Note, however, that css selectors perform better than XPath.
There are multiple number of ways to locate your WebElement in Selenium WebDriver.
But always remember all are based on you attribute or combination of HTML tags so case could be any of them
1- First way is using id
2- 2nd one is Name
3- Class Name
4- Some time you can used Tagname
5- Some time linkText
6- Some time partial link text
7- Using xpath
8- Using css selector
So in you case we need to take help of Xpath and Css Selector
So xpath of you elements
Syntax : //[#attribute ='value of selected tag']
Example
id1: //a[#model-id='Id1']
id2: //a[#model-id='Id2']
For both element following are the css Selector
Syntax [attribute ='value']
id1:
a[model-id='Id1']
id2:
a[model-id='Id2']
http://www.slideshare.net/weekendtesting/css-selector-29312437
http://www.slideshare.net/weekendtesting/locators-in-selenium-bnt-09
Thanks a lot for help. I have used following code to overcome above mentioned issue,
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("a[data-model-id='c5']"))).Click();
With above code, I am able to click on the button.
Thanks again for your help and knowledge sharing.
Amit
You can locate by using xpath.
WebElement ele = driver.findElement(By.xpath("//*[#class='Some big class name']"));
there is difference between findElements and findElement.
FindElement: findElement returns a single element.
FindElements : returns a list of same element.As in this example there are multiple classes with same class name , so use driver.findElements method .
driver.findElements will return a list of all elements with that class name .
Now , you have list of all elements but you want only one of the element.
So iterate over list to get a single element out of a list.
List<WebElement> elementList= driver.FindElement(By.ClassName("Some.big.class.name"));
Iterator itr = elementList.iterator();
while(itr.hasNext())
{
WebElement element = itr.next();
if(element.getAttribute("model-id").equals("Id1")){
element.click();
break;
}//if block ends here
}//while loop ends here
You can also use XPATH , if nothing works
To identify the elements in selenium there are multiple ways.
To see the details please refer BY Class.
Try to find the way which can identify the element uniquely. Start with id if available and if nothing works go for XPATH. XPATH is slower than id and CSS selector.
Initially I tried with below code:
WebElement admin = driver.findElement(By.xpath("//a[text()='Users']"));
and then also with
WebElement usrs = driver.findElement(By.xpath("//html/body/div[2]/div[3]/div[1]/form/div[4]/div[2]/div[1]/a[linktext()='Users']"));
The above codes didn't identify the object (link) in web page.
Please help me to resolve this issue.!
Try for the first 'a' element containing 'Users'.
WebElement admin = driver.findElement(By.xpath("//a[contains(text),'Users']"));
or for exact match
WebElement admin = driver.findElement(By.xpath("//a[.='Users']"));
and
"//body/div[2]/div[3]/div[1]/form/div[4]/div[2]/div[1]/a[contains(text),'Users']"
If you're going the xpath route, make it easy on yourself. Inspect element with Chrome then right click on the element in the chrome console and "copy xpath". That will give you the exact xpath and you don't have to worry about any "contains" or anything else. That's what the [1] is for.