使用硒时,即使切换框架,也无法在确实在框架弹出窗口上单击任何东西

时间:2019-08-03 14:15:58

标签: java selenium

我正在尝试确实使用Selenium WebDriver提交应用程序。

我已经输入jobType和jobLocation,按了搜索,打开了第一个结果,然后在新选项卡中,切换了焦点,然后按了Apply。现在,出现一个弹出窗口,将页面上以前的1个iframe更改为2。我切换到第二个iframe,然后尝试将键发送到“名称”的输入文本字段,但不存在输入字段

        driver.get("https://www.indeed.com");       
        WebElement what = driver.findElement(By.id("text-input-what"));

        what.sendKeys("Java Programmer");
        WebElement where = driver.findElement(By.id("text-input-where"));

        Thread.sleep(500);
        for (int i = 0; i < 20; i++) {
            where.sendKeys(Keys.BACK_SPACE);
        }
        where.sendKeys("Toronto, ON");
        WebElement submit = driver.findElement(By.xpath("//button[@class='icl-Button icl-Button--primary icl-Button--md icl-WhatWhere-button']"));

        submit.click();

        WebElement thirdElement = driver.findElement(By.xpath("/html[1]/body[1]/table[2]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[2]/div[9]"));      

        thirdElement.click();

        System.out.println(driver.getWindowHandle());
        ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
        driver.switchTo().window(tabs.get(1));  
        Thread.sleep(3000);
        WebElement apply = driver.findElement(By.xpath("//button[@class='icl-Button icl-Button--branded icl-Button--md']//div[@class='jobsearch-IndeedApplyButton-contentWrapper'][contains(text(),'Apply Now')]"));
        System.out.println(driver.findElements(By.tagName("iframe")).size());
        apply.click();
        driver.manage().window().maximize();


        Thread.sleep(3000);
        System.out.println(driver.getWindowHandle());
        driver.switchTo().frame(1);
        System.out.println(driver.getWindowHandle());
//      Thread.sleep(3000);
        System.out.println(driver.findElements(By.tagName("iframe")).size());

        WebElement inputName = driver.findElement(By.xpath("//input[@id='input-applicant.name']"));
        inputName.sendKeys("Adam Smith");

html code on ref pageTarget indeed Page

预期结果是我可以在文本字段中输入文本,而实际情况是它给出了错误消息:

  • 线程“ main”中的异常org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“ method”:“ xpath”,“ selector”:“ //input[@id='input-applicant.name']“} (会话信息:chrome = 76.0.3809.87)

1 个答案:

答案 0 :(得分:0)

要实现inputName,必须将框架切换两次,请尝试使用以下代码来实现您的意思:

driver.get("https://www.indeed.com");
WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.elementToBeClickable(By.id("text-input-what")));
WebElement what = driver.findElement(By.id("text-input-what"));
what.sendKeys("Java Programmer");

WebElement where = driver.findElement(By.id("text-input-where"));
for (int i = 0; i < 20; i++) {
    where.sendKeys(Keys.BACK_SPACE);
}
where.sendKeys("Jakarta");

WebElement submit = driver.findElement(By.xpath("//button[@class='icl-Button icl-Button--primary icl-Button--md icl-WhatWhere-button']"));
submit.click();

wait.until(ExpectedConditions.elementToBeClickable(By.className("title")));
WebElement thirdElement = driver.findElement(By.className("title"));
thirdElement.click();

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'Apply Now')]")));
WebElement apply = driver.findElement(By.xpath("//*[contains(text(),'Apply Now')]"));
apply.click();

driver.manage().window().maximize();

Thread.sleep(1000);

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("[src^='https://apply.indeed.com/indeedapply/x'")));

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("[src^='https://apply.indeed.com/indeedapply/resume']")));

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='input-applicant.name']")));
WebElement inputName = driver.findElement(By.xpath("//input[@id='input-applicant.name']"));
inputName.sendKeys("Adam Smith");

Thread.sleep(2000);