Reputation: 3
I am having some problems while using selenium select. The application on which I am working has got a time out of 60 min. So I cannot keep the loop running as it takes 2 hours to complete.
So what i am doing is I logout and login again in order to get a new session.
But after I login, the Selenium Select is not selecting the visible text from the dropdowns. The loop works perfectly though.
If anyone have any idea on how it will work fine please share.
Thanks, Varun.
Here is my code;
package MyPractice;
import java.util.LinkedList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class WorkingWithTestDropDowns {
public static WebDriver dev;
public static void main(String[] args) throws Exception {
dev = new FirefoxDriver();
dev.manage().window().maximize();
dev.get("location of the test page");
sHomeURL = dev.getCurrentUrl();
GetDropDowns();
System.out.println(sListItemTextListBox1);
System.out.println(sListItemTextListBox2);
System.out.println(sListItemTextListBox3);
System.out.println(sListItemTextListBox4);
DropDownValidation();
}
public static List<String> sListItemTextListBox1 = new LinkedList<String>();
public static List<String> sListItemTextListBox2 = new LinkedList<String>();
public static List<String> sListItemTextListBox3 = new LinkedList<String>();
public static List<String> sListItemTextListBox4 = new LinkedList<String>();
public static List<WebElement> weOptions;
public static WebElement weOptionFinder;
public static void GetDropDowns() {
// --------------------------------------------------------------Texttool1
weOptionFinder = dev.findElement(By.id("tool1"));
weOptionFinder.click();
weOptions = weOptionFinder.findElements(By.xpath(".//option"));
for (WebElement getOptions : weOptions) {
sListItemTextListBox1.add(getOptions.getText());
}
dev.findElement(By.id("tool1")).click();
// --------------------------------------------------------------End
// --------------------------------------------------------------Texttool2
weOptionFinder = dev.findElement(By.id("tool2"));
weOptionFinder.click();
weOptions = weOptionFinder.findElements(By.xpath(".//option"));
for (WebElement getOptions : weOptions) {
sListItemTextListBox2.add(getOptions.getText());
}
dev.findElement(By.id("tool2")).click();
// --------------------------------------------------------------End
// --------------------------------------------------------------Texttool3
weOptionFinder = dev.findElement(By.id("tool3"));
weOptionFinder.click();
weOptions = weOptionFinder.findElements(By.xpath(".//option"));
for (WebElement getOptions : weOptions) {
sListItemTextListBox3.add(getOptions.getText());
}
dev.findElement(By.id("tool3")).click();
// --------------------------------------------------------------End
// --------------------------------------------------------------Texttool4
weOptionFinder = dev.findElement(By.id("tool4"));
weOptionFinder.click();
weOptions = weOptionFinder.findElements(By.xpath(".//option"));
for (WebElement getOptions : weOptions) {
sListItemTextListBox4.add(getOptions.getText());
}
dev.findElement(By.id("tool4")).click();
// --------------------------------------------------------------End
}
public static Select oList1;
public static Select oList2;
public static Select oList3;
public static Select oList4;
public static String sHomeURL;
public static int iAttempts;
public static List<WebElement> rect = new LinkedList<WebElement>();
public static void DropDownValidation() {
int iCountGeo = 0;
iAttempts = 0;
while (iAttempts < 5) {
try {
oList1 = new Select(dev.findElement(By.id("tool1")));
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
iAttempts = 0;
while (iAttempts < 5) {
try {
oList2 = new Select(dev.findElement(By.id("tool2")));
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
iAttempts = 0;
while (iAttempts < 5) {
try {
oList3 = new Select(dev.findElement(By.id("tool3")));
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
iAttempts = 0;
while (iAttempts < 5) {
try {
oList4 = new Select(dev.findElement(By.id("tool4")));
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
for (String sTextGeo : sListItemTextListBox1) {
iAttempts = 0;
while (iAttempts < 5) {
try {
oList1.selectByVisibleText(sTextGeo);
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
for (String sTextMetric : sListItemTextListBox2) {
iAttempts = 0;
while (iAttempts < 5) {
try {
oList2.selectByVisibleText(sTextMetric);
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
for (String sTextTime : sListItemTextListBox3) {
iCountGeo++;
iAttempts = 0;
while (iAttempts < 5) {
try {
oList3.selectByVisibleText(sTextTime);
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
for (String sTextProduct : sListItemTextListBox4) {
iAttempts = 0;
while (iAttempts < 5) {
try {
oList4.selectByVisibleText(sTextProduct);
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
System.out.println("Hello...!");
}
if (iCountGeo == 2) {
iCountGeo = 0;
dev.navigate().to("http://www.google.com");
dev.navigate().to(sHomeURL);
continue;
}
}
}
}
}
}
Below is the code for the Test page:
<html>
<head><title>Testing</title></head>
<body>
<center>
<h1>Testing Page</h1>
<hr>
<br><br>
<form id = "testform">
<table width = "600" border = "2">
<tr>
<td align = "center" width = "200" bgcolor = "lightgrey">
Control Type
</td>
<td align = "center" width = "400" bgcolor = "lightgrey">
Form Control
</td>
</tr>
<tr>
<td align = "center" width = "200" bgcolor = "white">
Listbox1
</td>
<td align = "left" width = "400" bgcolor = "white">
<select id = "tool1">
<option>a</option>
<option>b</option>
<option>c</option>
<option>d</option>
<option>e</option>
<option>f</option>
<option>g</option>
<option>h</option>
<option>i</option>
<option>j</option>
<option>k</option>
<option>l</option>
<option>m</option>
<option>n</option>
<option>o</option>
<option>p</option>
<option>q</option>
<option>r</option>
<option>s</option>
<option>t</option>
<option>u</option>
<option>v</option>
<option>w</option>
<option>x</option>
<option>y</option>
<option>z</option>
</select>
</td>
</tr>
<tr>
<td align = "center" width = "200" bgcolor = "white">
Listbox2
</td>
<td align = "left" width = "400" bgcolor = "white">
<select id = "tool2">
<option>a1</option>
<option>b1</option>
<option>c1</option>
<option>d1</option>
<option>e1</option>
<option>f1</option>
<option>g1</option>
<option>h1</option>
<option>i1</option>
<option>j1</option>
<option>k1</option>
<option>l1</option>
<option>m1</option>
<option>n1</option>
<option>o1</option>
<option>p1</option>
<option>q1</option>
<option>r1</option>
<option>s1</option>
<option>t1</option>
<option>u1</option>
<option>v1</option>
<option>w1</option>
<option>x1</option>
<option>y1</option>
<option>z1</option>
</select>
</td>
</tr>
<tr>
<td align = "center" width = "200" bgcolor = "white">
Listbox3
</td>
<td align = "left" width = "400" bgcolor = "white">
<select id = "tool3">
<option>a2</option>
<option>b2</option>
<option>c2</option>
<option>d2</option>
<option>e2</option>
<option>f2</option>
<option>g2</option>
<option>h2</option>
<option>i2</option>
<option>j2</option>
<option>k2</option>
<option>l2</option>
<option>m2</option>
<option>n2</option>
<option>o2</option>
<option>p2</option>
<option>q2</option>
<option>r2</option>
<option>s2</option>
<option>t2</option>
<option>u2</option>
<option>v2</option>
<option>w2</option>
<option>x2</option>
<option>y2</option>
<option>z2</option>
</select>
</td>
</tr>
<tr>
<td align = "center" width = "200" bgcolor = "white">
Listbox4
</td>
<td align = "left" width = "400" bgcolor = "white">
<select id = "tool4">
<option>a3</option>
<option>b3</option>
<option>c3</option>
<option>d3</option>
<option>e3</option>
<option>f3</option>
<option>g3</option>
<option>h3</option>
<option>i3</option>
<option>j3</option>
<option>k3</option>
<option>l3</option>
<option>m3</option>
<option>n3</option>
<option>o3</option>
<option>p3</option>
<option>q3</option>
<option>r3</option>
<option>s3</option>
<option>t3</option>
<option>u3</option>
<option>v3</option>
<option>w3</option>
<option>x3</option>
<option>y3</option>
<option>z3</option>
</select>
</td>
</tr>
</table>
</form>
</center>
</body>
Upvotes: 0
Views: 1668
Reputation: 2019
Okay, seems that after reloading the page you actualy get the StaleElementReferenceException
@ these lines, and you do nothing when you catch it.
for (String sTextTime : sListItemTextListBox3) {
iCountGeo++;
iAttempts = 0;
while (iAttempts < 5) {
try {
oList3.selectByVisibleText(sTextTime);
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
}
The poin is, that defining oList
's in DropDownValidation()
method is misplaced. This part
if (iCountGeo == 2) {
iCountGeo = 0;
dev.navigate().to("http://www.google.com");
dev.navigate().to(sHomeURL);
continue;
}
works only for sListItemTextListBox3
and sListItemTextListBox4
options selection. After you reload the page, oList3
and oList4
become stale. So, I would recommend to move these parts (oList3 = new Select(dev.findElement(By.id("tool3")));
) right before the oList3.selectByVisibleText(sTextTime);
inside the try-catch block for each of these for
statements.
for (String sTextProduct : sListItemTextListBox4) {
iAttempts = 0;
while (iAttempts < 5) {
try {
oList4 = new Select(dev.findElement(By.id("tool4"))); //here
oList4.selectByVisibleText(sTextProduct);
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
PS Tried this on Python, but the logic's the same.
UPDATE BTW, by doing so you also should be able to get rid of these statements in DropDownValidation()
:
iAttempts = 0;
while (iAttempts < 5) {
try {
oList1 = new Select(dev.findElement(By.id("tool1")));
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
Upvotes: 1