I have tried with below coding to obtain the text from table.It works fine.But ,i want pick the data from first column alone.How can i grab the text from first column. .
WebElement tableContents = pubDriver.findElements(By.id("view_table"));
List<WebElement> rows=tableContents.findElements(By.tagName("tr"));
for(int rnum=0;rnum<rows.size();rnum++)
{
List<WebElement> columns=rows.get(rnum).findElements(By.tagName("td"));
for(int cnum=0;cnum<columns.size();cnum++)
{
System.out.println(columns.get(cnum).getText());
}
}
I have tried with below coding ,i didn't get the text from first column
columns.get(0).getText();
you can do like this....
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://localhost:8081/TestXmlDisplay/tabletest.html");
WebElement tableContents = driver.findElement(By.tagName("table"));
List<WebElement> rows=tableContents.findElements(By.tagName("tr"));
for(int rnum=0;rnum<rows.size();rnum++)
{
List<WebElement> columns=rows.get(rnum).findElements(By.tagName("td"));
System.out.println(columns.get(0).getText());
}
// driver.quit();
Related
I am creating script but did not get perfect call. please help me how to create script dropdown value store in array. I an unable to call in selenium web drive.
Dropdown menu scrrenshot: http://prntscr.com/100j0km
Dropdown menu list: http://prntscr.com/100j1og
Thanks
WebElement facultyId = driver.findElement(By.id("faculty_id"));
facultyId.click(); // to make the options to appear
Thread.sleep(500); // if not using implicit wait
List<String> values = new ArrayList<String>();
List<String> texts = new ArrayList<String>();
List<WebElement> options = facultyId.findElements(By.tagName("option"));
for (WebElement option: options) {
values.add(option.getAttribute("value"));
texts.add(option.getText());
}
String[] collectedValues = (String[]) values.toArray();
String[] collectedTexts = (String[]) texts.toArray();
Check Below Answer I have used this script
// options to appear
driver.findElement(By.xpath("//*[#id=\"s2id_autogen3\"]")).click(); // options to appear
Thread.sleep(6000);
driver.findElement(By.xpath("//*[#id=\"s2id_autogen3\"]")).sendKeys("Office Department"); //pass the sendkeys
Thread.sleep(6000);
//Use the Using explicit wait
WebDriverWait Faculties = new WebDriverWait(driver, 120);
WebElement Departments =Faculties.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("*//Put the xpath for finding keyword"))); //Put the xpath for finding keyword
Departments.click(); //Click on finding keyword
I have a requirement . I am reading file from a dynamic web page , and the values which i require from the webpage lies within
<td>
, and this is visible when i inspect this element . So my question is , is it somehow possible to print the data contained in the inspect element using java?
Using JSOUP. Here is the cookbook
ArrayList<String> downServers = new ArrayList<>();
Element table = doc.select("table").get(0);
Elements rows = table.select("tr");
for (int i = 1; i < rows.size(); i++) {
Element row = rows.get(i);
Elements cols = row.select("td");
// Use cols.get(index) to get the data from td element
}
I found the solution to this one , leaving this answer in case if anyone stuck into this in future.
To print whatever you see inside inspect element can be tracked down using selenium.
Here's the code which i used `
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.whatever.com");
Thread.sleep(1000);
List<WebElement> frameList = driver.findElements(By.tagName("frame"));
System.out.println(frameList.size());
driver.switchTo().frame(0);
String temp=driver.findElement(By.xpath("/html/body/table/thead/tr/td/div[2]/table/thead/tr[2]/td[2]")).getText();
read here for more .
Not able to Select the month for Date of Birth.
Code I am using is:
driver.findElement(By.xpath(".//*[#id = 'BirthMonth']/div")).click();
Thread.sleep(3000);
WebElement months = driver.findElement(By.xpath(".//[#id='BirthMonth']/div[2]/div[#id=':1']"));
Thread.sleep(2000);
months.click();
I also tried with by using DropDownList case. But Not able to set any Month.
Please Say me the Solution.
You can use keyboard event replace mouse.
WebElement birthMonth = driver.findElement(By.id("BirthMonth"));
birthMonth.click();
Actions action = new Actions(driver);
action.sendKeys(Keys.DOWN).sendKeys(Keys.ENTER).perform();
We can use sendKeys directly:
driver.findElement(By.xpath(".//*[#id='BirthMonth']/div[1]")).sendKeys("July");
You can wrap this up in a function
public void selectBirthMonth(int monthIndex)
{
driver.findElement(By.cssSelector("#BirthMonth > div")).click();
driver.findElements(By.cssSelector("#BirthMonth > div.goog-menu > div.goog-menuitem")).get(monthIndex - 1).click();
}
and then call it like
selectBirthMonth(9); // 1 = January
WebElement month = wd.findElement(By.xpath("//[#id=\"BirthMonth\"]/div[1]"));
month.click();
Thread.sleep(3000);
//wd.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//fetch months from the dropdown and store it in a list
List<WebElement> month_dropdown = wd.findElements(By.xpath("//[#id=\"BirthMonth\"]/div[2]/div/div"));
//iterate the list and get the expected month
for (WebElement month_ele:month_dropdown){
String expected_month = month_ele.getAttribute("innerHTML");
// Break the loop if match found
if(expected_month.equalsIgnoreCase("August")){
month_ele.click();
break;
}
}
It is not drop down value , you have to click on drop down arrows and then click on any value which you have to pass from script.
Code is below:
System.setProperty("webdriver.chrome.driver", "E:/software and tools/chromedriver_win32/chromedriver.exe");
WebDriver driver= new ChromeDriver();
//FirefoxDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://accounts.google.com/SignUp");
Thread.sleep(5000);
driver.findElement(By.xpath(".//*[#id='BirthMonth']/div[1]/div[2]")).click();
driver.findElement(By.xpath(".//*[#id=':7']/div")).click();
it is workable code for Birthmonth
Please find below link for same type of Question
Not able to select the value from drop down list by using Select method in Selenium
I am trying to test a trip planner website using selenium webdriver.
Say for e.g. I am testing cleartrip.com.
How to select a city from the lookup field using selenium?
For instance, I have to select the source city.
If I type DE, it gives me options like "Delhi, "Dehradun" based on what I have typed and out of these options, I have to select say Delhi.
How to achieve it using Selenium? Kindly suggest.
I am new to selenium and any help would be appreciated.
This code works for me
WebDriver driver = new FirefoxDriver();
driver.get("http://www.cleartrip.com/");
WebElement From = driver.findElement(By.id("FromTag"));
From.sendKeys("Del");
WebElement autoComplete = driver.findElement(By.id("ui-id-1"));
try{
(new WebDriverWait(driver, 5/*sec*/)).
until(ExpectedConditions.presenceOfElementLocated((By.cssSelector("li.list")))); }
catch(org.openqa.selenium.TimeoutException e){
System.out.println(e.getMessage());
}
List<WebElement> autoCompleteList = autoComplete.findElements(By.className("list"));
if(autoCompleteList.size()==0) {
System.out.println("autoComplete list NOT found");
}
else {
System.out.println("autoComplete list Found with elements: "+autoCompleteList.size());
}
for (WebElement ac: autoCompleteList){
if(ac.getText().contains("Delhi")){
ac.click();
break;
}
}
driver.close();
Please note this is just example, please improve the code adding verifications that objects not a null, remove the hardcoded strings, etc.
You can simply achieve it by putting up some wait and then clicking the desired city, like:
public CurrentPage selectFrom(String cityCode, String cityName){
driver.findElement(By.id("FromTag")).sendKeys(cityCode);;
new WebDriverWait(driver, 5).until(
ExpectedConditions.elementToBeClickable(
By.xpath(".//*[#id='ui-id-1']//a[contains(.,'" + cityName + "')]")))
.click();
return this;
}
I'm using Selenium Webdriver in Java. I have a table, and I like to get my hands on the last cell on the first row, and the last cell of last row. I manage to get one of them
WebElement table =driver.findElement(By.className("dataTable"));
List <WebElement> rows = table.findElements(By.tagName("tr"));
WebElement firstrow= rows.get(0);
WebElement lastrow= rows.get(rivit.size()-1);
List <WebElement> firstcells = firstrow.findElements(By.tagName("td"));
List <WebElement> lastcells = lastcell.findElements(By.tagName("td"));
firstcell.get(6).getText());
This is because I'm locating td-tags twice. Any hints how to get both cells nicely? I have no identifiers in my rows or cells.
You can use xpath to get the elements:
WebElement lastCellInFirstRow = driver.findElement(By.xpath("table[#class='dataTable']//tr[1]//td[last()]"));
WebElement lastCellInLastRow = driver.findElement(By.xpath("table[#class='dataTable']//tr[last()]//td[last()]"));
Here's the xpath specification. You can play with xpath here.
You can try to make it with cssSelectors:
String cssLast="table[class='dataTable']>tr:first-child>td:last-child"
String cssFirst="table[class='dataTable']>tr:last-child>td:last-child"
it will be smt like that;
driver.findElement(By.cssSelector(cssLast)).getText();
driver.findElement(By.cssSelector(cssFirst)).getText();
another approach is using js:
String getText(cssSel){
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\""+cssSel+"\");");
stringBuilder.append("return x.text().toString();") ;
String res= (String) js.executeScript(stringBuilder.toString());
}
text1=getText(cssLast);
text2=getText(csscssFirst);
But always make sure that you located elements properly (e.g. using firepath, firebug addon in firefox)
The TableDriver extension (https://github.com/jkindwall/TableDriver.Java) offers a nice clean way to handle things like this. If your table has headers, you can (and should) identify the cell column by its header text, but in case you don't have headers, you can still do something like this.
Table table = Table.createWithNoHeaders(driver.findElement(By.className("dataTable")), 0);
WebElement firstRowLastCell = table.findCell(0, table.getColumnCount() - 1);
WebElement lastRowFirstCell = table.findCell(table.getRowCount() - 1, table.getColumnCount() - 1);