Java 如何使用selenium webdriver关闭弹出窗口和在线广告

Java 如何使用selenium webdriver关闭弹出窗口和在线广告,java,selenium,selenium-webdriver,popover,bootstrap-popover,Java,Selenium,Selenium Webdriver,Popover,Bootstrap Popover,我正在尝试使用在线教程等学习SeleniumWebDriver 我正在努力克服这个问题,就是要关闭这件府绸 使用: 笔记本电脑:Alienware 操作系统:Windows 10 64位 浏览器:Firefox 51.0.1(32位) Eclipse:版本:Neon.2发行版(4.6.2)构建id:20161208-0600 Selenium Webdriver:Java 3.0.1 2016-10-18 ` 预期结果:Selenium Webdriver将打开firefox浏览器,加载d

我正在尝试使用在线教程等学习SeleniumWebDriver

我正在努力克服这个问题,就是要关闭这件府绸


使用: 笔记本电脑:Alienware 操作系统:Windows 10 64位 浏览器:Firefox 51.0.1(32位) Eclipse:版本:Neon.2发行版(4.6.2)构建id:20161208-0600 Selenium Webdriver:Java 3.0.1 2016-10-18


`


预期结果:Selenium Webdriver将打开firefox浏览器,加载decision.co.uk网页,在第一个字段中插入“Selenium”,在第二个字段中插入“London”,点击搜索按钮,在控制台和驱动程序窗口中获取标题和作业计数值

实际结果:Selenium Webdriver将打开firefox浏览器,加载decision.co.uk网页,在第一个字段中插入“Selenium”,在第二个字段中插入“London”,点击搜索按钮,在url字段中停止焦点,其他情况不会发生


我尝试了一些解决方案,但都没能奏效 ()

e、 g





注意:不完全确定我的xpath是否正确编写,仍在学习中

这些似乎都不起作用。我读了一些关于SeleniumWebDriver不处理引导弹出窗口的文章,不确定这是否正是我的情况,或者你们中是否有人找到了解决方案

希望得到解决方案和/或建议:)


非常感谢您。

您的代码通常看起来很好(除了使用
Thread.Sleep()
,我将在一分钟内介绍

基本上,在这些情况下,您要做的是右键单击对话框的close X,并将其视为页面上的任何其他元素。找到X的定位器,在这种情况下,它还有一个id,prime popover close按钮,我们可以使用。您只需使用id抓住该元素,然后单击它以关闭弹出窗口。我已经简化了d下面的代码

driver.get("https://www.indeed.co.uk/");
driver.findElement(By.id("what")).sendKeys("Selenium");
driver.findElement(By.id("where")).sendKeys("London");
driver.findElement(By.id("fj")).click();
driver.findElement(By.id("prime-popover-close-button")).click();
如果您不想在搜索页面上测试UI(输入文本并单击按钮),您可以直接导航到url,甚至输入您自己的关键字(如果愿意)。请参阅下面的代码

String what = "selenium";
String where = "london";
driver.get("https://www.indeed.co.uk/jobs?q=" + what + "&l=" + where);
driver.findElement(By.id("prime-popover-close-button")).click();

现在回到
Thread.Sleep()
。这种形式的等待通常是一种不好的做法。你可以对细节做一些研究,但足以说明它不灵活。如果你睡眠10秒,元素在25毫秒内出现,你已经等待了很长时间,而你并不需要。阅读
WebDriverWait
ExpectedConditions
。而你不需要在这里,您最终需要等待,这些是等待的最佳实践。

看起来我正在做与您相同的教程:)我遇到了与您完全相同的问题,并尝试了几乎所有您所做的操作,在找到此线程之前单击关闭按钮并杀死弹出框

问题似乎在于,单击“查找作业”后,Selenium无法立即关闭popover。必须设置“wait.until..”以等待弹出框出现,以便我们可以关闭它。以下是我所做的:

package com.indeed.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait;      //**and this

public class IndeedJobSearch {

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub

    //Create firefox driver to drive the browser

    WebDriver driver;

    System.setProperty("webdriver.gecko.driver", "C:\\Users\\BURRITOBEAST\\Downloads\\jars\\geckodriver-v0.14.0-win64\\geckodriver.exe");

    driver = new FirefoxDriver();

    WebDriverWait wait = new WebDriverWait(driver,10); //**and this. 10 is the number of seconds it'll wait before an error is thrown.

    //Open Indeed homepage
    driver.get("http://www.indeed.com");
    //Find the 'what' field and enter "selenium"
    driver.findElement(By.id("what")).sendKeys("Selenium");
    //Find the 'location' field and enter "San Diego, CA"
    driver.findElement(By.id("where")).clear();
    driver.findElement(By.id("where")).sendKeys("San Diego, CA");
    //Find the 'findjobs' button and click on it
    driver.findElement(By.id("fj")).click();

    wait.until(ExpectedConditions.elementToBeClickable(By.id("prime-popover-close-button"))); //**this is where the magic happens

    //Thread.sleep(1000); **tested my idea first using a sleep. then found the wait method after. plus, i want to avoid sleeps if possible to make things speedy.
    driver.findElement(By.id("prime-popover-close-button")).click();

    //From the job search results page, get page title and jobs count msg

}
}

早上好@BurritoBandit,哇,非常感谢。这就是问题所在!元素无法立即使用,这就是我无法关闭它的原因!!!!谢谢,这让我想到了这些问题,我觉得我学到了一些对未来非常重要的东西!你真棒!我将阅读更多关于预期条件和驱动程序等的内容:)Hello@JeffC,正如我之前在问题上提到的那样,我确实尝试过。。。尝试了一些解决方案,但没有成功。Yogesh Rathi和BurritoBandit有一个很好的观点,我所要做的就是添加一个等待popover可用的等待,然后单击。(我以前没想过……我承认我还是个新手)。无论如何,谢谢你关于睡眠线程的建议,我会读更多的。:)我发布的代码是工作代码。我自己也试过,效果不错。Hello@JeffC,在我把问题发到这里之前,我已经试过了。我尝试了很多解决方案,但都不奏效。即使现在,我已经创建了一个新的JavaProject来再次尝试您的代码。就我而言,它不起作用。但是,如果我有等待,直到预期的条件,那么其余的代码工作。“导入org.openqa.selenium.support.ui.WebDriverWait;”“WebDriverWait wait=new WebDriverWait(driver,10);“wait.until(ExpectedConditions.elementtobelickable(By.id)(“prime popover close button”);“我不知道为什么它在你的电脑上工作,没有等待,它在我的电脑上就不工作。老实说,我仍然很感激你花了这么多时间来帮助我。正如我所说,一个建议总是受欢迎的(阅读睡眠线程)。我在这方面太新手了,还不明白为什么它对你有效而对我无效。(可能是popover在您这边更快?还是eclipse或java控制台更耐心/更慢?您使用的是哪个版本?可能是internet连接更快?没有意义。)
driver.findElement(By.xpath("//*[@id='prime-popover-close-button']/a/img")).click();
driver.get("https://www.indeed.co.uk/");
driver.findElement(By.id("what")).sendKeys("Selenium");
driver.findElement(By.id("where")).sendKeys("London");
driver.findElement(By.id("fj")).click();
driver.findElement(By.id("prime-popover-close-button")).click();
String what = "selenium";
String where = "london";
driver.get("https://www.indeed.co.uk/jobs?q=" + what + "&l=" + where);
driver.findElement(By.id("prime-popover-close-button")).click();
package com.indeed.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait;      //**and this

public class IndeedJobSearch {

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub

    //Create firefox driver to drive the browser

    WebDriver driver;

    System.setProperty("webdriver.gecko.driver", "C:\\Users\\BURRITOBEAST\\Downloads\\jars\\geckodriver-v0.14.0-win64\\geckodriver.exe");

    driver = new FirefoxDriver();

    WebDriverWait wait = new WebDriverWait(driver,10); //**and this. 10 is the number of seconds it'll wait before an error is thrown.

    //Open Indeed homepage
    driver.get("http://www.indeed.com");
    //Find the 'what' field and enter "selenium"
    driver.findElement(By.id("what")).sendKeys("Selenium");
    //Find the 'location' field and enter "San Diego, CA"
    driver.findElement(By.id("where")).clear();
    driver.findElement(By.id("where")).sendKeys("San Diego, CA");
    //Find the 'findjobs' button and click on it
    driver.findElement(By.id("fj")).click();

    wait.until(ExpectedConditions.elementToBeClickable(By.id("prime-popover-close-button"))); //**this is where the magic happens

    //Thread.sleep(1000); **tested my idea first using a sleep. then found the wait method after. plus, i want to avoid sleeps if possible to make things speedy.
    driver.findElement(By.id("prime-popover-close-button")).click();

    //From the job search results page, get page title and jobs count msg

}
}