I am trying to find out the no. of orders in the following picture using
//*[#id='past-orders-tab']/div[contains(#class,'physical')]
but it is returning zero using .size() ( storing in List[WebElement])
.When i picked one order using findelement() ,it is working fine. Here's the page source code
How to correct it?
You should try using WebDriverWait to wait until there is at least one element present on a web page as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> ord = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("#past-orders-tab > div.physical")));
Related
Could you please help me for finding an element in webdriver:
Suppose we have two drop downs one is client and second facility. Also, without selecting client we cannot select facility as its disabled.
We've selected client value from drop down.
Now I've written a script for a new tab.
After that, I've to find facility field through ID but it shows element is not found, then could you please help me for the same?
..
Attached is the screen shot for your reference.
Could you please check?
Continue with previous question Select options from Autopopulate text boxes using Selenium webdriver
When you are using same cssSelector second time, it locates first dropdown element which is invisible at that time. you need to use more specific locator as below using label text :-
WebElement facility = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("facility")))
facility.sendKeys("Ho")
List<WebElement> facilityOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(".//div[label[text() = 'Ordering Location']]/ul[#class = 'typeahead dropdown-menu']//a")))
facilityOptions.get(0).click()
Full working example code :-
driver.get("https://bioceptbetaweb.azurewebsites.net/Account/Login");
driver.manage().window().maximize()
WebDriverWait wait = new WebDriverWait(driver, 60)
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))).sendKeys("ajay.kumar#technossus.com");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password"))).sendKeys("Ajay#123");
wait.until(ExpectedConditions.elementToBeClickable(By.id("btn-Login"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Place a New Order"))).click();
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loaderDiv")));
//this sleep is required because after invisibility of loader focus goes to first input which is Requisition Number
//If you are filling form from first input no need to for this sleep
//if you want to input directly to client field need to sleep to avoid focus first
Thread.sleep(3000);
WebElement client = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("client")));
client.sendKeys("Ho");
List<WebElement> dropdownOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(".//div[label[text() = 'Client']]/ul[#class = 'typeahead dropdown-menu']//a")));
dropdownOptions.get(0).click();
WebElement facility = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("facility")));
facility.sendKeys("Ho");
List<WebElement> facilityOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(".//div[label[text() = 'Ordering Location']]/ul[#class = 'typeahead dropdown-menu']//a")));
facilityOptions.get(0).click();
I am not able to click on 'Albums' in Facebook.
The HTML is Albums how to locate the element 'Albums' in selenium web driver.
I tried with using driver.findelement(By.xpath(span[#class="_3sz"]) showing error as element not found
And, the html looks the following:
<span class="_3sz">Albums</span>
If I am understanding your problem correctly then that xpath you mentioned returns more than one elements. Use a text based search which is more easier and specific.
driver.findelement(By.xpath("//*[.='Albums']").click();
And, here . is used to directly point to the parent element. Additional wait might be needed to wait for the element to interact. Also, I am assuming you are trying to click the element.
EDIT
Driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
Driver.Navigate().GoToUrl("http://www.facebook.com");
Driver.Manage().Window.Maximize();
Driver.FindElement(By.CssSelector("#email")).SendKeys("your email");
Driver.FindElement(By.CssSelector("#pass")).SendKeys("your pass");
Driver.FindElement(By.CssSelector("[type='submit'][value='Log In']")).Click();
Driver.FindElement(By.CssSelector(".fbxWelcomeBoxName")).Click();
Driver.FindElement(By.XPath("//*[.='Photos']")).Click();
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[.='Albums']")));
Driver.FindElement(By.XPath("//*[.='Albums']")).Click();
By albumname = By.XPath("//strong[.='2014']"); //this should be your album name. In my case it's 2014
wait.Until(ExpectedConditions.ElementExists(albumname));
Driver.FindElement(albumname).Click();
wait.Until(ExpectedConditions.ElementExists(By.CssSelector(".fbPhotoAlbumHeader.fbPhotoAlbumOptionsPresent [type='file']")));
Driver.FindElement(By.CssSelector(".fbPhotoAlbumHeader.fbPhotoAlbumOptionsPresent [type='file']")).SendKeys(#"D:\Users\Saifur\Desktop\FacebookPicture\150232_585410621540701_1836495431_a.jpg");
wait.Until(ExpectedConditions.ElementExists(By.CssSelector(".pvm.phl.footerBox.uiBoxWhite")));
Driver.SwitchTo().ActiveElement();
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("[name='postPhotosButton']")));
Driver.FindElement(By.CssSelector("[name='postPhotosButton']")).Click();
Notice mine is C#
It always best practice to follow this sequence while selecting elements.
1) ID
2) CSS
3) XPath (This will have some issues with different browsers specially IE)
In this case, considering this is no other span class with same name. It would have work like this "span._3sz". Simple and powerful.
Before posting this, I've thoroughly researched all possible syntax available for this and got no avail.
The closest I had was using this code where the dropdown actually appeared but didn't select my desired option:
new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select")));
driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select")).sendKeys("Local Move");
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select"))).click();
I spent a significant amount of time today figuring this one out but I really failed on this one big time.
WebElement dropDownListBox = driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select"));
Select clickThis = new Select(dropDownListBox);
Thread.sleep(5000L);
clickThis.selectByValue("1078");
It seems I needed Thread.sleep to let the system display the options completely before selenium can find the option I want for it to choose from.
Thanks guys!
To select a value from drop down. You need to use Select class from web driver.
driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select")).sendKeys("Local Move");
Instead of the above line use this
WebElement ele = driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select"));
Select dropdown = new Select(ele);
dropdown.selectByVisibleText("Local Move");
// Can select the dropdown using `index` and `value`
dropdown.selectByValue("Local Move");
dropdown.selectByIndex("1234");
To select a value from dropdown you should find the dropdown element by id then by value in the dropdown.
Try this:
new Select(driver.findElement(By.id("Dropdownid"))).selectByVisibleText("Text name");
I am creating a framework for the data validation using selenium. The issue I am struggling with is I want to locate the element "td"(html tag) within element "tr"(html tag) . This is the code I have written.
Iterator<WebElement> i = rows.iterator();
While(i.hasnext()){
List<WebElement> columns = row.findElements(By.tagName("td"));
for(WebElement s:columns)
{
System.out.println("columnDetails : "+s.getText().toString());
}
if(columns.isEmpty())
{
ElementNotFoundException e = new ElementNotFoundException("No data in table");
throw e;
}
Iterator<WebElement> j = columns.iterator();// does some other work
ClusterData c = new ClusterData(); // does some other work
ClusterDataInitializer.initUI(c, j, lheaders); // does some other work
CUIData.put(c.getCN(), c); // does some other work
}
Now the issue with this is:
I am trying to fetch the data from the rows(see table data) in arraylist and use that arraylist further. Currently whats happening is the data for column header is fetched at start of which I have no use.I only want the rows's data. I am not able to determine the proper way to collect the data of table rows only.
if xPath of the table will help you understand it properly then here are the details :
Table header xPath of cluster name column:
/html/body/table/tbody/tr[2]/td[2]/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div[2]/div/div/div[2]/div[2]/div/table/tbody/tr/td[2]/div/div[2]
Table row (Table Data) xPath of test cluster 01:
/html/body/table/tbody/tr[2]/td[2]/div[2]/div/div/div[2]/div/div/div[2]/div/div/div[2]/div/div/div/div/div/div[2]/div/div/div[2]/div/div/div[3]/div[2]/div/table/tbody/tr/td[2]/div/div/a
Please let me know if you need anything else.
I am using the following code to extract row data from table.
List<WebElement> rows = getElement(driver,sBy,"table_div_id").findElements(By.tagName("tr"));
where sBy = By.id and table_div_id = id of div in which table is present. This extracts all the rows into arraylist and then i am using code to extract the row data into another arraylist. It is where I am stuck.
Each row from the table is in its own "table" tag so following things are not working :-
List<WebElement> rows = driver.findElements(By.xpath("//div[#id = 'table_div_id']//tr"));
List<WebElement> columns = row.findElements(By.xpath("./td"));
or the approach I used for the previous release of product i.e.
List<WebElement> columns = row.findElements(By.tagName("td"));
So, I used following approach which enabled me to capture all of the visible rows from the table.
List<WebElement> columns = row.findElements(By.xpath(".//table[#class='gridxRowTable']/tbody/tr"));
But after that I faced another issue that is since this table was implemented using dojo, the scrolling was impossible and Selenium was only able to capture the visible rows , so to overcome this I zoomed out in the browser using selenium. This is how i achieved my goal of getting the data.I believe others might have provided me answer if i would have shared some more details. Still , sorry about that and hope my answer helps you all.
instead of
List<WebElement> columns = row.findElements(By.tagName("td"));
try using
List<WebElement> columns = row.findElements(By.xpath("./td"));
Check if this helps. This should give you the td elements. If I have not understood your issue, let me know.
You can use this way-
driver.findElement(By.Xpath("//table[#id=\"table1\"]/tbody/tr[2]/td[1]"));
Regards,
Anuja
Do you have selenium IDE installed? Perform storeText operation on the row you want to retrieve, then xpath will get populated in IDE. There will be multiple xpaths; the most reliable is xpath:position, use that to capture your rows.
And use firebug for better visibilty of your AUT.
Firebug and Selenium IDE are the most basic component of Selenium Framework development.
You can manipulate xpath as you want.
I was working on reading mails from gmail using webdriver and in between I hit upon this difference between By.id and By.tagname.
I am trying to get access to a "table" whose id is ":pg". So I could
Either use By.id(":pg")
OR use By.tagname("table") and search for an element with id :pg
Here is the code for both cases.
By.id:
WebDriver webDriver = new FirefoxDriver();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver = webDriver.switchTo().frame("canvas_frame");
WebElement table1 = webDriver.findElement(By.id(":pg"));`
Above code, I directly get the element which has id ":pg"
By.tagname:
WebDriver webDriver = new FirefoxDriver();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> tables = webDriver.findElements(By.tagName("table"));
for(WebElement table2: tables){
String id = table2.getAttribute("id");
System.out.println("id: "+ id);
if(id != null && id.equals(":pg")){
System.out.println("FOUND IT!!!");
}
}
Above code, I find all elements with the tagname of table and then see which one has the id ":pg".
Both these code snippets are essentially doing the same but using different ways(By.id or By.tagname). However, the first snippet of code which uses By.id always succeeds while the second snippet of code which uses By.tagname fails almost always. (It will work with additional waiting however)
Why is this difference between By.id and By.tagname?
Thanks,
Chris.
The :pg element is not present on the page initially.
Using By.Tag, selenium will not wait for the :pg element.
Because By.Id example is more specific, selenium will continue checking if the :pg element exists until the implicit wait (5 seconds) times out.
By.Tag is not specific at all. On findElements(By.tagName("table"), Selenium will return an array of all the tables that are present immediately after the page loads. As the :pg element is not present yet, it will not be in the array.
To answer your question, yes it is better to use By.Id because:
1. It is more specific.
2. Saves lines of code
3. Forces selenium to wait for the element to exist.
It is better to use By.Id according to your question.
By.tag is not used for specific data, it actually will search and return an array of all the tables with the specified tag name. On the other hand using id you can get the appropriate output for identifying/locating element.
Go for tag only if id, name or class is not specified and the best way can be By.cssSelector if no element is found.
Thanks