How to identify Elements using text attribute by Selenium Webdriver in TestNG - java

I have just started working on Selenium Webdriver and wanted to try out a command that will identify any element based on the text it shows in the UI.
The HTML Structure of the attribute is shown below.
<div class="cuesLoginProductName">Select One</div>
i want to identify the div name on the Text "Select One". Can this be done in Selenium Webdriver. I have done it before in Ruby using the command
#browser.div(:text=>'Select One').present?
Please let me know how i can identify the element.
Thanks and Regards
Sushanth.G

Try using xpath //div[text()='Select One']
Driver.findElements(By.xpath("//div[text()='Select One']"));

Related

cant find the element using classname in selenium java

I'am using selenium framework to test my own website. Im trying to click on specific icon which using anchor tag. I have java selenium code to click but couldn't click. Tried many xpath, css selectors, class name and names. but didn't worked. But can run the script and it is opening the chrome and navigating to entered domain but the clicking option is not working
above code I need to click nav-twitter class anchor option . which will create another tab in chrome to show my twitter page. but after running the app .it is only navigating to the page domain and nothing works.
So, This is my code where I have added. until the maximize everything works but not the anchor tag
This kind of error im getting when running the script in chrome. Please anyone let me know where I have been wrong here or is there are any way to make it happen. Thanks in advance.
Whenever you are initiating webdriver make sure to add implicit wait statement, so it can wait for sometime before looking for objects. Add below statement right after chromedriver initialization and your code should work without any issues.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Your locator is slightly off. In your code you are looking for an element with tagName as twitter which doesn't exists. Instead you can use either of the following locator strategies:
Using cssSelector:
WebElement navTwitter = driver.findElement(By.cssSelector("a.nav-twitter[name='twitter']"));
navTwitter.click();
Using xpath:
WebElement navTwitter = driver.findElement(By.xpath("//a[#class='nav-twitter' and name='twitter']"));
navTwitter.click();
You have used wrong selector/locator here. "twitter" is not tag name, Tag name is the value from less than symbol to the first space or greater than symbol. Words like div, p, a, span are the tag names.
So here a is the tag name and name, class, href are attribute which you can use while forming xpath or css locators. So you can create multiple locators here:
By using class name attribute
By.className("nav-twitter");
By creating xpath using name attribute
By.xpath("//a[#name='twitter']");]
By creating xpath using class attribute
By.xpath("//a[#class='nav-twitter']");
By creating css locator using class attribute.
By.cssSelector("a[class*='nav-twitter']");
By using dot symbol in css selector with class name
By.cssSelector("a.nav-twitter");
Similarly you can create css selector with name too. Since there are multiple tags, so you can not use a tag directly and If you want to use By.tagName("a") then it will click on first tag present on the page
Note: We use dot with class name and Hash (#) with Id name while forming css selectors.

How to deal with Dynamically changing class names in HTML source code while automating using Selenium WebDriver

How to deal with Dynamically changing "class names" or "ids" in HTML source code while automating using Selenium WebDriver
Example Application : GMAIL
if you know the cell index then using XPath or cssSelector will solve the issue. Here in the above screenshot it is clear that the text "Wordpress" is in 5th cell of each table row/so you can use below XPath to get the particular cell.
List<WebElement> cellValue = driver.findElements(By.xpath(".//table/descendant::tr/following-sibling::td[5]"));
The Selenium mechanism for locating elements (org.openqa.selenium.By) offers other methods of locating elements.
Example: name, partialLinkText, css selector, xpath selector, tagname, linkText.
This tutorial from Selenium Easy contains some easy to understand examples.
If you want to locate <span name="WordPress">WordPress</span> element, You can locate this element using By.name() as below :-
driver.findElement(By.name("WordPress"));

How to find the HTML element id of text field pop up window?

I am trying to automate browser using Selenium in Java. However I am unable to find the element ID of a text field in pop up window to call it in Selenium Java.
The text field whos HTML element ID that I am trying to find is Project (as seen in attached photo)
Selenium script:
driver.findElement(By.id("project-name")).sendKeys("TEST: Automatation by Selenium");
Any suggestions?
This works for me:
driver.findElement(By.id("project-field")).sendKeys("TEST: Automatation by Selenium");
Are you sure that the id is project-name and not project-field? In my Jira template project-field is used as id.

Not able to find my element which is text between span tag

I'm using Selenium Web Driver for automation and with this new app, I'm not able to find element by normal method.
My code looks like:
driver.findElement(By.xpath("html/body/div[1]/md-sidenav/md-list[1]/md-list-item[2]/button")).click();
And I want to click on 'Views'. Need some help, I have tried following methods for xpath:
//span[contains(#class, 'ng-binding ng-scope') and normalize-space()='Data']
//div[1]/md-sidenav/descendant::span[text()='Data']/button ---- no success
When I'm using :
driver.findElement(By.xpath("html/body/div[1]/md-sidenav/md-list[1]/md-list-item[2]/button")).click();
Above code works but I don't want to use item[x], because they will changes very often.
Please help!
Thanks!

How to access dynamic text within HTML icon tag with selenium WebDriver?

<span class="class name">16</span>
The "16" is dynamic and I don't know how to access it using Selenium Webdriver in java. I've tried By.xpath() and it didn't work but I feel like cssSelector would be more robust in this situation. could someone tell me how to access the 16. I'm writing a method to check the expected value to the given value. The 16 is part of an icon. I don't think that will make a difference because the cssSelector should still do the job.
You would do something like:
WebDriver driver = new FirefoxDriver();
String content = driver.find_element_by_class_name("class name").getText();
If there are multiple elements with the same class name you can use find_elements_by_class_name and iterate through them.
You could add a "data-" tag to the span element: example <span class="class name" data-hook="some.element.number">16</span>
Then your locator would look like this --> driver.findElement(By.cssSelector("[data-hook='some.element.number']");

Categories

Resources