使用selenium select时遇到一些问题。 我工作的应用程序有60分钟的时间。 所以我无法保持循环运行,因为它需要2个小时才能完成。
所以我正在做的是我注销并再次登录以获得新的会话。
但是在我登录后,Selenium Select没有从下拉列表中选择可见文本。 这个循环完美无缺。
如果有人知道如何正常工作,请分享。
谢谢, VARUN。
这是我的代码;
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;
}
}
}
}
}
}
以下是测试页面的代码:
<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>
答案 0 :(得分:1)
好的,似乎在重新加载页面之后,你实际上得到<{em} {@ 1}} @这些行,当你抓住它时你什么都不做。
StaleElementReferenceException
重点是,在for (String sTextTime : sListItemTextListBox3) {
iCountGeo++;
iAttempts = 0;
while (iAttempts < 5) {
try {
oList3.selectByVisibleText(sTextTime);
break;
} catch (StaleElementReferenceException e) {
}
iAttempts++;
}
}
方法中定义oList
的位置是错误的。这部分
DropDownValidation()
仅适用于if (iCountGeo == 2) {
iCountGeo = 0;
dev.navigate().to("http://www.google.com");
dev.navigate().to(sHomeURL);
continue;
}
和sListItemTextListBox3
选项选项。重新加载页面后,sListItemTextListBox4
和oList3
会变得陈旧。因此,我建议将这些部分(oList4
)移到try {catch}块中的oList3 = new Select(dev.findElement(By.id("tool3")));
之前,以便这些oList3.selectByVisibleText(sTextTime);
语句的每个。
for
PS在Python上尝试过,但逻辑是一样的。
更新 BTW,通过这样做,你也应该能够在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++;
}
中删除这些陈述:
DropDownValidation()