在使用Selenium webdriver自动化脚本时,.isDisplayed()
命令在if else语句中不起作用。如果在 if 语句下条件为真,则它工作正常。但是,如果条件不为 true,则代码不会移动到 else 语句。
package module17;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Dice {
static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\Automation Software\chromedriver_win32\chromedriver_update.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.get("http://www.dice.com/");
Thread.sleep(15000);
driver.findElement(By.xpath("//*[@alt='Close']")).click();
driver.findElement(By.xpath("//*[@id='search-field-keyword']")).sendKeys("Selenium WebDriver");
WebElement Place =driver.findElement(By.xpath("//input[@placeholder='Location']"));
Place.clear();
driver.findElement(By.xpath("//button[contains(text(),'Find Tech Jobs')]")).click();
WebElement PageNumber = driver.findElement(By.xpath("//*[@id='dice_paging_top']//*[contains(text(),'2')]"));
int i=2;
while(i<=44)
{
try
{
if(driver.findElement(By.xpath("//*[@id='dice_paging_top']//*[contains(text(),'"+i+"')]")).isDisplayed() )
{
driver.findElement(By.xpath("//*[@id='dice_paging_top']//*[contains(text(),'"+i+"')]")).click();
Thread.sleep(5000);
i= i+5;
}
else if(driver.findElement(By.xpath("//*[@id='dice_paging_top']//a[@title='Go to next page']")).isDisplayed() )
{
driver.findElement(By.xpath("//*[@id='dice_paging_top']//a[@title='Go to next page']")).click();
}
else
{
driver.quit();
}
}
catch (Exception e)
{
System.out.println("Exception");
}
}
}
}
实际上isDisplayed()
工作正常。问题出在您的循环中。在第一个 if 条件下,您用 i+5
递增了值 因此,值 7 在下一次迭代中的 xpath 中不匹配,这就是它在异常中移动的原因。
您需要在尝试捕获块中管理其他条件。替换下面的循环代码并尝试
int i=5;
while(i<=44)
{
try
{
if(driver.findElement(By.xpath("//*[@id='dice_paging_top']//*[contains(text(),'"+i+"')]")).isDisplayed() )
{
driver.findElement(By.xpath("//*[@id='dice_paging_top']//*[contains(text(),'"+i+"')]")).click();
Thread.sleep(5000);
i+=5;
}
}catch(Exception e)
{
try
{
if(driver.findElement(By.xpath("//*[@id='dice_paging_top']//a[@title='Go to next page']")).isDisplayed() )
{
driver.findElement(By.xpath("//*[@id='dice_paging_top']//a[@title='Go to next page']")).click();
}
}
catch(Exception e2)
{
driver.quit();
}
}
}
在第二次迭代中,I 值设置为 7,在 xpath 中此值为 I,则当前页面上不存在元素。如果页面上不存在该元素,则 drive.findElement 将抛出 NoSuchElementException,因此它根本不会输入到您的 elseif 块中,我相应地修改了您的代码(更新下面的解决方案并最终提供我的解决方案(。
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Dice {
static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\Automation Software\chromedriver_win32\chromedriver_update.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.get("http://www.dice.com/");
Thread.sleep(15000);
driver.findElement(By.xpath("//*[@alt='Close']")).click();
driver.findElement(By.xpath("//*[@id='search-field-keyword']")).sendKeys("Selenium WebDriver");
WebElement Place =driver.findElement(By.xpath("//input[@placeholder='Location']"));
Place.clear();
driver.findElement(By.xpath("//button[contains(text(),'Find Tech Jobs')]")).click();
WebElement PageNumber = driver.findElement(By.xpath("//*[@id='dice_paging_top']//*[contains(text(),'2')]"));
int i=2;
while(i<=44)
{
try
{
if(driver.findElement(By.xpath("//*[@id='dice_paging_top']//*[contains(text(),'"+i+"')]")).isDisplayed() )
{
driver.findElement(By.xpath("//*[@id='dice_paging_top']//*[contains(text(),'"+i+"')]")).click();
Thread.sleep(5000);
i+=1;
}
}
catch (Exception e)
{
try{
if(driver.findElement(By.xpath("//*[@id='dice_paging_top']//a[@title='Go to next page']")).isDisplayed() )
{
driver.findElement(By.xpath("//*[@id='dice_paging_top']//a[@title='Go to next page']")).click();
i+=1;
}
}
catch(Exception e1){
driver.quit();
}
}
}
}
}
如果您的目标是浏览所有职位,那么下面是我的简短代码。
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Dice {
static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\Automation Software\chromedriver_win32\chromedriver_update.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.dice.com/");
Thread.sleep(15000);
driver.findElement(By.xpath("//*[@alt='Close']")).click();
driver.findElement(By.xpath("//*[@id='search-field-keyword']")).sendKeys("Selenium WebDriver");
WebElement Place =driver.findElement(By.xpath("//input[@placeholder='Location']"));
Place.clear();
driver.findElement(By.xpath("//button[contains(text(),'Find Tech Jobs')]")).click();
while(true)
{
List<WebElement> nextPage = driver.findElements(By.xpath("//*[@id='dice_paging_top']//a[@title='Go to next page']"));
if(nextPage.size()>0&&nextPage.get(0).isEnabled()){
nextPage.get(0).click();
}
else{
break;
}
}
driver.quit();
}
}
尝试一下,如果您有任何疑问,请告诉我