繁体   English   中英

Selenium工具无法通过xpath识别元素

[英]Selenium tool is not able to identify the element by xpath

我正在尝试使naukri.com应用程序自动化。

成功登录后,我需要根据技能,位置,经验和薪水来搜索工作。借助xpath,我能够识别出第一个元素(技能,名称,公司)。以下是相同的xpath代码

//div[@id='skill']

但是,当我运行脚本时,无法识别该元素并且脚本失败。仅添加该元素不会落在任何框架中,请帮助我解决问题并附上丝网印刷以供参考 在此处输入图片说明

-这是参考代码-

public class naukri {
@Test
public void pagelaunch() throws InterruptedException
 {
 WebDriver driver = new FirefoxDriver();
 driver.get("http://www.naukri.com");
 driver.manage().window().maximize();
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 String parent = driver.getWindowHandle();
 //close all the pop ups
 Set<String> pops=driver.getWindowHandles();
 {
 Iterator<String> it =pops.iterator();
 while (it.hasNext()) {

     String popupHandle=it.next().toString();
     if(!popupHandle.contains(parent))
     {
     driver.switchTo().window(popupHandle);
     System.out.println("Pop Up Title: "+                                                                                   driver.switchTo().window(popupHandle).getTitle());
     driver.close();
     }
 }
 }
 driver.switchTo().window(parent);

 //to click on login button and proceed to login to the application
 driver.findElement(By.xpath("//a[@title='Jobseeker Login']")).click();

    for (String winhandle:driver.getWindowHandles())
    {
        driver.switchTo().window(winhandle);

    }


    driver.findElement(By.xpath("//a[@id='uSel']")).click();
             driver.findElement(By.xpath("//input[@id='uLogin']")).sendKeys("anand_qa");
    driver.findElement(By.xpath("//input[@id='pLogin']")).clear();
    driver.findElement(By.xpath("//input[@id='pLogin']")).sendKeys("test@1234");
    driver.findElement(By.xpath("//button[@class='blueBtn']")).click();
     driver.switchTo().window(parent);

     Thread.sleep(5000);
     WebElement element = driver.findElement(By.xpath("//a[@title='Search Jobs']"));
     Actions action = new Actions(driver);
     action.moveToElement(element).build().perform();
     driver.findElement(By.xpath("//a[@title='Jobs by Skill']")).click();
     Thread.sleep(5000);

     List<String> browserTabs = new ArrayList<String> (driver.getWindowHandles());

     driver.switchTo().window(browserTabs.get(1));
     //Below are the two elements which are not identified by the tool but the same gets identified thru fire bug 
     driver.findElement(By.xpath("//div[@id='skill']")).sendKeys("testing");
     driver.findElement(By.xpath("//div[@id='location']")).sendKeys("bangalore");

}

这对我有用:

Class<?> cls = Class.forName("org.openqa.selenium.firefox.FirefoxDriver");
WebDriver driver = (WebDriver) cls.newInstance();
driver.get("http://naukri.com");
WebElement webElement = driver.findElement(By.xpath("//div[@id='skill']"));
System.out.println(webElement.isEnabled());

输出为:

真正

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM