无法访问动态网页中的元素

时间:2018-12-03 14:56:25

标签: selenium selenium-webdriver

嗨,我尝试访问动态网页中的元素,但是我不能。 我在下面添加了代码。

我已经使用了相对xpath

网址为https://jobs.dxc.technology/page/show/Student-and-New-Grads

public static void main(String[] args) throws InterruptedException {
    WebDriver driver;
    System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver.exe");
    driver = new FirefoxDriver();
    driver.manage().deleteAllCookies();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
    driver.navigate().to("https://jobs.dxc.technology/page/show/Student-and-New-Grads");
    Thread.sleep(8000);

    try {

         ((JavascriptExecutor)driver).executeScript("scroll(0,2650)");
         Thread.sleep(3000);

        //Textbox for First name(i have used relative xpath )
         driver.findElement(By.xpath("//input[@data-friendly-name,'s_FirstName']")).sendKeys("raju");
         //Textbox for last  name
         driver.findElement(By.xpath("//input[@data-friendly-name,'s_LastName']")).sendKeys("123456");
         //Textbox for email 
         driver.findElement(By.xpath("//input[@data-friendly-name,'s_Email']")).sendKeys("raju12@gmail.com");
         //Textbox for phone number 
         driver.findElement(By.xpath("//input[@data-friendly-name,'s_MobileNumber']")).sendKeys("9626092023");
         //drop down for area of interest
         Select interest =new Select(driver.findElement(By.xpath("//select[starts-with(@id,'contactForm_f') and @size='4']")));
         interest.selectByVisibleText("Applications – JC");
         //drop down for location
         Select location =new Select(driver.findElement(By.xpath("//select[@data-friendly-name,'s_Country']")));
         location.selectByVisibleText("India");
         //radio button
         driver.findElement(By.xpath("//select[starts-with(@id,'contactForm_f') and @type,'radio']")).click();
         //join button           
         driver.findElement(By.name("contactForm$cmd0")).submit();


    } catch (Exception e) {

        e.printStackTrace();
    }
}

}

1 个答案:

答案 0 :(得分:0)

可以尝试的方法:使用javascriptexecutor时,必须处理内联单引号才能正确找到路径。

'//*[contains(@class=\'is-loading\')'
'//*[contains(@id=\'hover-gear\')'