[英]For loop with a size 3 WebElement List clicking on the same link after first instance
我在我們網站的便當盒組中有3個鏈接。 如果鏈接文本符合我的條件,我需要單擊特定的鏈接(無論如何我都需要單擊所有3個鏈接,並且在單擊它們之后需要執行不同的操作)。 我在for循環中編寫了if循環來實現這一點。
當我執行此操作時,對於第一個循環(即i = 0),它單擊正確的鏈接。 對於第二個循環(i = 1),我希望它選擇第二個鏈接。 但是它單擊了第三個鏈接。 對於第三個循環,也單擊第三個鏈接。 不確定我在這里缺少什么。
public void SeasonalLinks() throws InterruptedException
{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List <WebElement> Seasonallnks = driver.findElements(By.xpath("//section[@id='seasonallinks']/ul/li/div/a"));
int sl = Seasonallnks.size();
System.out.println("Total no.of seasonal Links: "+sl);
int i=0;
for (i=0;i<sl;i++)
{
List <WebElement> Seasonallnks1 = driver.findElements(By.xpath("//section[@id='seasonal-links']/ul/li/div/a"));
String sl1 = Seasonallnks1.get(i).getText();
if (sl1.equalsIgnoreCase("Start a provider search"))
{
Seasonallnks1.get(i).click();
WebDriverWait pshome = new WebDriverWait(driver,20);
pshome.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")));
Thread.sleep(5000);
driver.findElement(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")).click();
}
else if (sl1.equalsIgnoreCase("Use pharmacy self-service tools"))
{
Seasonallnks1.get(i).click();
WebDriverWait phome = new WebDriverWait(driver,20);
phome.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")));
Thread.sleep(5000);
WebDriverWait iswait1 = new WebDriverWait(driver,30);
iswait1.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='acc-spinner-container]")));
driver.findElement(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")).click();
}
else if (sl1.equalsIgnoreCase("Start rewarding yourself today"))
{
Seasonallnks1.get(i).click();
WebDriverWait hhome = new WebDriverWait(driver,20);
hhome.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")));
driver.findElement(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']")).click();
}
}
一些反饋...為了使您的代碼更具可維護性並節省精力,您應遵循DRY原則 (消除重復性代碼)。 如果您看一下if-else
鏈,那里會有很多重復。 每次單擊相同的鏈接時,請等待元素可單擊並單擊它。 對於單個元素,實際上只有一件獨特的事情要做。
應避免使用Thread.sleep()
(谷歌查看完整說明)。 您已經在使用WebDriverWait
,這是一個好習慣,因此,如果需要,可以添加另一個WebDriverWait
以避免Thread.sleep()
使用。
您有兩個集合, Seasonallnks
和Seasonallnks1
,它們都存儲相同的元素,但是第二個實例看起來像在定位器中有錯字。 我將兩者結合在一起。
在驅動程序的整個生命周期中, implicitlyWait()
超時僅需設置一次,並且可能應在此函數之外設置。
int i = 0;
的聲明int i = 0;
可以/應該在for
循環聲明中完成。
您聲明了兩次等待,但是您可以重用第一次等待,以使代碼更簡潔。
如果多次使用定位器,請考慮將其聲明為By
類型的變量,以方便重用。 它避免了拼寫錯誤,並且在需要時更易於更改,因為它只需要在一個地方進行更改。
wait.until()
返回等待的元素,因此當您等待元素可單擊然后打算單擊它時,您可以在末尾添加.click()
,例如wait.until(...).click();
您還應該努力擁有更多描述性的變量名。 它會在幾個月后或其他需要閱讀您的代碼並弄清楚它在做什么的人中為您提供幫助。
希望您能從下面的清理代碼中看到通過使用DRY並應用上面列出的其他一些建議可以節省多少行。
public void SeasonalLinks() throws InterruptedException
{
By seasonalLinksLocator = By.xpath("//section[@id='seasonallinks']/ul/li/div/a");
List<WebElement> seasonalLinks = driver.findElements(seasonalLinksLocator);
System.out.println("Total no.of seasonal Links: " + seasonalLinks.size());
WebDriverWait wait = new WebDriverWait(driver, 20);
for (int i = 0; i < seasonalLinks.size(); i++)
{
String seasonalLinkText = seasonalLinks.get(i).getText();
seasonalLinks.get(i).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='nav-home']/a[@aria-label='home button']"))).click();
if (seasonalLinkText.equalsIgnoreCase("Use pharmacy self-service tools"))
{
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='acc-spinner-container]")));
}
seasonalLinks = driver.findElements(seasonalLinksLocator);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.