Reputation: 11
Question:
There are 3 Text Boxes in the webpage without id, name but they have same class name. How can I locate the element without using Xpath. What is the basic or the simplest way of doing it.
Is there way of locating the elements in a better way?
Upvotes: 0
Views: 32145
Reputation: 3594
If using Microsoft Edge Driver
Right click your element, select Inspect.
In inspect window, right click it again Copy -> Copy full XPath
Then..
In Selenium (if using python)
myElem_1b = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, 'your XPATH')))
Upvotes: 1
Reputation: 83
You can find these textboxes with CSSSelector. for example:- ' List elements = wdriver.findElementsBycssSelector("tagname[classname='something']");' and then iterate over list to get a particular text box.
Upvotes: 0
Reputation: 1956
As your Question says not to use 'id','name' and 'Xpath' and in the Answer you mentioned to use Xpaths to find input Box Labels which might made the Interviewer not happy with the answer. But coming to the other Techniques which we can use for Web-Element identification are as follow:
By Classname
By LinkText
By Partial Linktext
By TagName
By CSS
Also Refer below Link which will help you to understand this techniques in more details.
How to locate Element without xpath, id, name in selenium WebDriver
Upvotes: 0
Reputation: 19
CSS is your one option.
View more details here at http://www.praveenjana.com/css-selectors-for-selenium-webdriver-automation-testers/
Upvotes: -3
Reputation: 5396
You can also locate element by followings :
1 - By tag name 2 - By css selector
In your case Css selector suitable. See below sample program for text box by css selector :
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/?gfe_rd=cr&ei=42uBVO_NEKXM8gfL4oHIAQ&gws_rd=ssl");
driver.findElement(By.cssSelector("#gbqfq")).sendKeys("test");
Upvotes: 1
Reputation: 815
If ordering was static and the class name was the same you could use cssSelector with nth-child().
As an example: You have an ordered list.
WebElement we = findElement(By.cssSelector("li:nth-child(1)"));
I'd probably use cssSelector and get back a list of elements the use a for each loop and cycle through clicking on or performing an action on the element that matched the requirement.
Upvotes: 2