我在使用硒選擇時遇到了一些問題。 我工作的應用程序有60分鐘的時間。 所以我不能保持循環運行,因爲它需要2個小時才能完成。Selenium在使用DropDown時選擇不工作,因爲它應該
所以我正在做的是我註銷並再次登錄,以獲得新的會話。
但是我登錄後,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>
你嘗試過什麼到目前爲止,和你得到了什麼異常?告訴我們你的代碼。 –
嘿亞歷山大,謝謝你的迴應,我已經發布了我的代碼.. – user3020548
什麼行爲的預期和你得到什麼? –