Java - Selenium Firefox 驱动程序 - 无法点击模式中的特定按钮

Java - Selenium Firefox Driver - cannot click on specific button in modal

我希望我的程序登录到 indeed.ca(这是有效的,只要您输入正确的用户凭据),导航到特定的职位发布(有效),单击第一个橙色申请按钮(工作),弹出一个模式。

然后我想点击出现的模式中的蓝色应用按钮。这是行不通的。我已经注释掉了我在这部分程序中的尝试。

如有任何帮助,我们将不胜感激。

import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;



public class Testing {

 public static void main(String[] args) throws IOException {
    //enter location of gecko driver   
    System.setProperty("webdriver.gecko.driver", "C:\Users\Padoga\Documents\geckodriver-v0.18.0-win64\geckodriver.exe");

    FirefoxDriver driver = new FirefoxDriver();

    //login works correctly (given that you use proper credentials)
    driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
    driver.findElement(By.xpath("//*[@id=\"signin_email\"]")).sendKeys("abc@gmail.com");
    driver.findElement(By.xpath("//*[@id=\"signin_password\"]")).sendKeys("enterPassword");
    driver.findElement(By.xpath("//*[@id=\"loginform\"]/button")).click();  

    //once logged in navigate to specific job
    driver.navigate().to("https://ca.indeed.com/cmp/KGHM-International-Ltd./jobs/Financial-Analyst-7a08f1634e7d5c5c");

    //clicking on first apply button(orange button) works correctly
    Thread.sleep(3000);
    driver.findElement(By.xpath("//*[@id=\"apply-state-picker-container\"]/div[1]/span[1]")).click();

    //below not working, trying to click on apply button(blue apply button) in popup modal
    //I've tried so many different xpaths and ids none seem to be triggering the apply button in modal 
    Thread.sleep(3000);
    driver.switchTo().frame("indeedapply-modal-preload-iframe");
    driver.findElement(By.id("apply")).click();

 } 
}

这里是各种html/javascript?我一直试图点击 即用作 By.id、By.xpath 或 By.className、none 正在工作 当我检查页面源代码时,下面的代码没有显示,只有当我检查单击橙色应用按钮后弹出的模态中的蓝色应用按钮时,我才会看到下面的代码:

<div class="button_outter" id="apply-div">
    <div class="button_inner">
        <input class="button_content" id="apply" type="submit" name="apply" value="Apply">
    </div>
</div>

我试过使用 Iframe 的切换,但在这种情况下不起作用。点击第一个应用按钮后,你能检查下面的发送密钥方法吗?

 Actions act = new Actions(driver);
 act.sendKeys(Keys.TAB, Keys.TAB, Keys.TAB, Keys.TAB, Keys.ENTER);

driver.findElement(By.xpath("//body")).sendKeys(Keys.TAB, Keys.TAB,Keys.TAB, Keys.TAB, Keys.ENTER);

更新:看来,第二条建议确实适用于这种情况。

希望这对您有所帮助。谢谢

您需要先切换到 iframe,然后在 Blue 应用 按钮上调用 click() 方法,如下所示:

    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'https://apply.indeed.com/indeedapply/resumeapply')]")));
    //perfrom other actions
    //finally click on Blue Apply button
    driver.findElement(By.xpath("//input[@id='apply']")).click();   

iframe 的索引顺序似乎是倒退的,至少我是这样认为的。我能够使用以下 java 代码单击 "Blue Apply Button":

driver.get("https://ca.indeed.com/cmp/KGHM-International-Ltd./jobs/Financial-Analyst-7a08f1634e7d5c5c");
wait = new WebDriverWait(driver, 10);
//on my screen I had to scroll down to the orange apply button
WebElement applyButton  = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("indeed-apply-button")));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", applyButton);
applyButton.click();
Thread.sleep(1000);
driver.switchTo().frame(1);
driver.switchTo().frame(0);
driver.findElement(By.id("applicant.name")).sendKeys("My First Name is");
driver.findElement(By.id("apply-div")).click();