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

时间:2015-06-12 07:12:15

标签: java xpath selenium-webdriver

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

成功登录后,我需要根据技能,位置,经验和薪水搜索工作。在xpath的帮助下,我能够确定第一个元素(技能,指定,公司)。下面是xpath代码对于相同的

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"); }

然而,当我运行脚本时,该元素未被识别且脚本失败。并且只是为了添加元素不会落入任何框架中。请帮助我解决问题和附加的屏幕打印参考enter image description here

- 这是参考代码 -

c = "mssql+pyodbc://sa:admin11$$@PROIMT01\SQLEXPRESS/master?driver=SQL+Server+Native+Client+11.0"
e = create_engine(c,echo=True)
con = e.connect()

con.execute("CREATE DATABASE xb;")
con.close()

1 个答案:

答案 0 :(得分:0)

这对我有用:

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());

输出是: