gpt4 book ai didi

java - 使用 selenium 时,尽管切换了框架,但确实无法在 Indeed 上弹出框架时单击任何内容

转载 作者:行者123 更新时间:2023-12-02 02:09:02 24 4
gpt4 key购买 nike

我正在尝试使用 Selenium WebDriver 提交申请。

我已输入工作类型和工作位置,按下搜索,打开第一个结果,然后在新选项卡中切换焦点,然后按下应用。现在,出现一个弹出窗口,将页面上之前的 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”:“//输入[@id='input-applicant.name']"}( session 信息:chrome=76.0.3809.87)

最佳答案

要实现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);

关于java - 使用 selenium 时,尽管切换了框架,但确实无法在 Indeed 上弹出框架时单击任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57339124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com