Find Element using Selenium Webdriver - selenium-webdriver

I am trying to find an element in the website that i am trying to automate but am unable to figure out how to fetch a particular element. I have provided the html below. Please provide some inputs. Thanks.
< div id="Troy_combine" class="sign_in_flow sign_in_flow_overlay troy_overlay epc_modal big_dialog modal_display" > < h3 class="migration"> Welcome back.< /h3>< h3 class="normal">Welcome to Disneyland< / h3>< p class="migration">It looks like you have a account that use the same address and password. Let’s combine them into a single account to make it easier to access both services.< /p>
Note: There are multiple "migration" classes. How will it know which one to pick up??
I am trying to fetch the text shown in Italics. Help required
Selenium Webdriver with Java

I would use a cssSelector like so:
String itext = driver
.findElement( By.cssSelector("div#Troy_combine p.migration")).getText();

Selecting via classes and IDs are always the easiest and most reliable. Do a By.className("migration").

findElement(By.cssSelector("div.migration")).getText();
I think you could select it by ID as well.

Related

Which locator to use? I am using xpath but it is not working

Which locator to use for my code?
I tried using Xpath but for some reason xpath is not working.
xpath:
//*[#id="ext-gen25"]/table/tbody/tr[1]/td[2]/a
driver.findElement(By.xpath("//*[#id='ext-gen25']/table/tbody/tr[1]/td[2]/a")).click();
Data Manager [eXchange]
By clicking on that hyperlink the system should navigate to X module.
If this Data Manager [eXchange] text is unique you could stick to it and omit the parent table:
//a[text()='Data Manager [eXchange]']
Demo:
If you need to match the link for the specific row it is more tricky but still possible, you will need to share your table code so we could come up with the right expression, an example one would be something like:
//table/tr/td[count(//table/tr/th[.='the header you're looking for']/preceding-sibling::th) + 1]/a[text()='Data Manager [eXchange]']
References:
XPath Tutorial
XPath Axes
XPath Operators & Functions
Also be aware of Table class which is a part of HtmlElements framework, it makes working with tables much easier

Selenium script for click the search box in office 365 webmail

when clicking the search field in office 365 webmail the xpath is
.//*#id='primaryContainer']/div[4]/div/div[1]/div[2]/div[1]/div[1]/div/div/div[1]/div[1]/div[1]/div[2]/div[2]/button
but selenium identifies for the first time and not for the second time.
is there any way to write selenium script using
<span class="_n_t ms-font-weight-semilight ms-font-color-neutralPrimary">Search Mail and People</span>
or any way to identify?
You can try finding it by the text it has.
//span[text() = 'Search Mail and People']
Try to use CSS Selector with #FindBy annotation instead of XPATH like:
#FindBy(css="[class='login_textfield textfield required email field normaltext']")
WebElement emailField;
Sorry, I'm not registered, so I cannot provide you deeper example, hope it helps you enough :)
See at:
http://screencast.com/t/rqa9dOoy9iq
Edit:
I saw that you uploaded some source, try:
#FindBy(css="[class='_n_t ms-font-weight-semilight ms-font-color-neutralPrimary']")
WebElement someWebElementVariable;

Are there any tools for recording web browser events for seleniumn2?

I am looking for something very simple. It can use the Selenium IDE recording tool, but it does not allow me to pick what kind of locators I get.
I want to use:
driver.findElement(By.className(str))
to locate things. All I need is something which watches which UI elements on a web page get clicked and writes out the class attributes of those tags.
If I use the Selenium IDE recording (and export to the right type of thing), I get:
#Test
public void testNav() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.name("3.1.1.5.1.1")).clear();
driver.findElement(By.name("3.1.1.5.1.1")).sendKeys("dan");
driver.findElement(By.name("3.1.1.5.1.5")).click();
driver.findElement(By.linkText("Products")).click();
driver.findElement(By.linkText("Categories")).click();
driver.findElement(By.linkText("Create a Category")).click();
driver.findElement(By.linkText("Cancel")).click();
driver.findElement(By.linkText("Products")).click();
driver.findElement(By.cssSelector("a.DisplayAdminProductsLink")).click();
driver.findElement(By.linkText("Product1")).click();
There are problems with this. First, it is not give me any By.className() calls. Why? Those first 3 calls will not help me. The framework I am using puts arbitrary things into the name. How can I get it to see the class attribute?
There actually are unique words in the class attribute of all of the above tags. I design my apps so that this is so. Yet it will not use them.
Earlier I asked:
Why is it doing a "driver.findElement().click()"? This is fragile and does not
end up working.
What I need is:
elt = driver.waitFor(By.className("c")); elt.click();
This will work reproducibly.....
I am considering to be removed from the question, as the findElement() code does work. You need to set a general time-out on the driver. It is not very obvious that this can be done, but it can.
So, continuing on....
I can go to the "Options" and change the order of the "Locator Builders" in eclipse. I can put "css" at the top. Then I get:
driver.findElement(By.cssSelector("input[name=\"3.1.1.5.1.1\"]")).clear();
driver.findElement(By.cssSelector("input[name=\"3.1.1.5.1.1\"]")).sendKeys("dan");
driver.findElement(By.cssSelector("input[name=\"3.1.1.5.1.5\"]")).click();
The tags are like:
<input class="form-control LoginUsernameField" ... />
But it does not see the class attribute.... Or I can do this manually.
Selenium looks for a unique identifier to identify elements in a webpage. classNames are a very less desired option for this purpose as they are generally not unique. Ids and names on the other hand are generally unique. This might be the reason why Selenium IDE is not selecting classNames and going for other identifiers.
Selenium IDE records user actions. You would have clicked on the element for Selenium IDE to identify it and that is why you are getting driver.findElement().click().
If you want to wait for element to wait you can try implicit wait.
When you want to use driver.findElement(By.className(str)), are you sure that there is one and only one element in the webpage that is associated with a className? If that is the case you can modify the webdriver code manually to use className.

Selenium WebDriver cannot locate element within an iframe, and throws NoSuchElementException

I realise there are several queries on here for this same problem but none of them provide a solution to my particular problem.
I am running a web driver test that tries to fill out a form for a mail website to find postcodes based on address details. I keep getting this error when trying to locate the first text box:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"#ctl00_BodyContent_txtBuildingNumber"}
I have used xpath and the id to try and locate the element but I keep getting the error. I can see that this element is present when the webdriver is running and I have been able to locate another text element on the page and enter text, but I keep getting this error for this field and other fields within the frame.
I am guessing that the problem must be to do with the fact that this field is part of an iFrame.
I have used implicit waits within the test but with no success. I still get the error.
By the sounds of it you'll need to first switch to the iframe element that contains the element that you want to interact with. (Although without seeing the relevant HTML this is a bit of an extrapolated guess).
driver.switchTo().frame();
eg:
driver.switchTo().frame(1);
driver.switchTo().frame(driver.findElement(By.id("id")));
When you've finished interacting with the elements within the frame, you'll need to switch back to the main webpage.
driver.switchTo().defaultContent();
Check your xpath .
try to use simple
driver.switchTo().frame(1);
with wait statement.
sorry a little correction the following worked for me
driver.switchTo().frame(driver.findElement(By.xpath("//*[#id='page-15']/div/p/iframe")));
//*[#id='page-15']/div/p/iframe is the xpath of the frame in which the button i was trying to click was located. (driver is of type WebDriver i.e WebDriver driver ) thank you
The following worked for me
driver.switchTo().frame(myd.findElement(By.xpath("//*[#id='page-15']/div/p/iframe")));
//*[#id='page-15']/div/p/iframe is the xpath of the frame in which the button I was trying to click.(driver is of type WebDriver i.e WebDriver driver)
thank you
you can use wait statement and after using the wait statement use simply
driver.swithcTo().frame();

Selenium web driver cannot recognize a link text

in one of the web app that I am automating, I find that I came across to some elements that have no ID or class or anything associated, or associated dynamically and won't be able to capture, therefore I have to use link text, for example, "Canada", and then when I do
driver.FindElements(By.LinkText("Canada")).Count > 0 ,
I won't get a "True" although "Canada" is right there. Does anybody came across this before and have any idea why it is?
Thank you!
Use
driver.switchTo().frame("Your LinkText Frame Name");
driver.FindElements(By.LinkText("Canada")).Count > 0 ;
and try.
I think it may work.
Are you sure "Canada" is indeed in a <a> tag ? By.LinkText is meant to search for anchor text
You can certainly create a XPath to search By.xpath. Something like "//*[contains(text(),'Canada')]" (this is the only xpath I can give you without knowing your HTML structure)

Resources