使用selenium webdriver自动执行脚本时, .is dispayed()命令在 if else语句中无效。 如果在if语句下条件为真,那么它工作正常。但如果条件不成立,则代码不会移动到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");
}
}
}
}
答案 0 :(得分:1)
实际上isDisplayed()
工作正常。问题在你的循环中。在first if条件中,您使用i+5
增加了值,因此在下一次迭代中,值x在xpath中不匹配,这就是它在Exception中移动的原因。
您需要在try catch块中管理if else条件。替换下面的循环代码并尝试
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();
}
}
}
答案 1 :(得分:0)
在第二次迭代中,我将值设置为7,在xpath中使用此值,当前页面上不存在元素。如果页面上不存在该元素,则drive.findElement将抛出NoSuchElementException,因此它赢得了# 39;完全输入你的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();
}
}
尝试一下,如果您有任何疑问,请告诉我