GVKun编程网logo

使用Selenium Java(Mac OSX)将Firefox浏览器置于最前面(firefox浏览器怎么设置主页)

21

在本文中,我们将详细介绍使用SeleniumJava的各个方面,并为您提供关于MacOSX将Firefox浏览器置于最前面的相关解答,同时,我们也将为您带来关于centos系统中安装使用seleniu

在本文中,我们将详细介绍使用Selenium Java的各个方面,并为您提供关于Mac OSX将Firefox浏览器置于最前面的相关解答,同时,我们也将为您带来关于centos 系统中安装使用selenium,firefox、Firefox更新后无法使用Selenium打开浏览器、java selenium+firefox环境搭建、mac 搭建 python+selenium+firefox 爬虫的有用知识。

本文目录一览:

使用Selenium Java(Mac OSX)将Firefox浏览器置于最前面(firefox浏览器怎么设置主页)

使用Selenium Java(Mac OSX)将Firefox浏览器置于最前面(firefox浏览器怎么设置主页)

我正在使用三个firefox驱动程序实例进行自动化。我需要将当前活动的firefox浏览器置于最前面,因为我正在使用一些robo类来进行某些操作。我曾在Mac中尝试过Google
chrome的Java脚本警报(相同操作),并且工作正常。在Windows中使用user32 lib。对于firefox
mac,其在后台显示警报,但网页不在最前面。

((JavascriptExecutor)this.webDriver).executeScript("alert(''Test'')");this.webDriver.switchTo().alert().accept();

我在Mac中用于chrome的上述代码。相同的代码正在工作,并显示有关Firefox的警报,但窗口未显示在最前面。

请建议在firefox中是否还有其他方法可以这样做。

答案1

小编典典

首先将窗口句柄存储在变量中,然后再使用它返回窗口。

//Store the current window handleString currentWindowHandle = this.webDriver.getWindowHandle();//run your javascript and alert code((JavascriptExecutor)this.webDriver).executeScript("alert(''Test'')"); this.webDriver.switchTo().alert().accept();//Switch back to to the window using the handle saved earlierthis.webDriver.switchTo().window(currentWindowHandle);

此外,您可以尝试在切换到窗口后最大化它,这也应该激活它。

this.webDriver.manage().window().maximize();

centos 系统中安装使用selenium,firefox

centos 系统中安装使用selenium,firefox

由于爬取的页面需要登录获取信息,所以需要模拟浏览器环境。

安装selenium
命令:pip install selenium

安装firefox
命令:yum install firefox
firefox --version,查看firefox的版本(60.7)

安装驱动
操作firefox浏览器需要用到驱动geckodriver
安装包地址:https://github.com/mozilla/ge...
上面有各种版本的驱动,此处下载的是v19,并解压放置在usr/bin中


错误提示

WebDriverException: Message: invalid argument: can''t kill an exited process

出现该错误,可能是因为驱动与浏览器的版本不兼容,降驱动版本比较方便。

https://stackoverflow.com/que...

Webdriver Exception:Process unexpectedly closed with status: 1

出现该错误,可能是因为在无界面的系统中启用浏览器,且没有设置headless选项。

解决方法:

from selenium import webdriver
from selenium.webdriver 
import FirefoxOptions
opts = FirefoxOptions()
opts.add_argument("--headless")
browser = webdriver.Firefox(firefox_options=opts)
browser.get(''http://example.com'')

https://stackoverflow.com/que...

WebDriverException: Message: ''geckodriver'' executable needs to be in PATH

解决方法:设置executable_path为驱动放置位置

webdriver.Firefox(executable_path=''/usr/bin/geckodriver'')

OSError: [Errno 20] Not a directory

webdriver.Firefox(''/usr/bin/geckodriver'')此处的错误为没有设置参数名

解决方法:webdriver.Firefox(executable_path=''/usr/bin/geckodriver'')

Firefox更新后无法使用Selenium打开浏览器

Firefox更新后无法使用Selenium打开浏览器

如何解决Firefox更新后无法使用Selenium打开浏览器?

:目前的解决方案是降级Firefox!运行此命令以获取可用Firefox版本的列表。

apt-cache show firefox | grep Version

我的结果:

Version: 47.0+build3-0ubuntu0.16.04.1
Version: 45.0.2+build1-0ubuntu1

安装:

sudo apt-get install firefox=45.0.2+build1-0ubuntu1

要保留此版本并禁止更新:

sudo apt-mark hold firefox

如果要取消保留Firefox版本并允许更新:

sudo apt-mark unhold firefox
sudo apt-get upgrade

解决方法

我在Ubuntu Desktop 16.04上使用Selenium
WebDriver,但无法打开浏览器。Firefox更新后出现以下错误(在此之前,所有方法都可以正常工作):

Traceback (most recent call last):
  File "test.py",line 6,in <module>
    driver = webdriver.Firefox()
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py",line 81,in __init__
    self.binary,timeout)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/extension_connection.py",line 51,in __init__
    self.binary.launch_browser(self.profile,timeout=timeout)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_binary.py",line 68,in launch_browser
    self._wait_until_connectable(timeout=timeout)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_binary.py",line 98,in _wait_until_connectable
    raise WebDriverException("The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor,check it for details.

java selenium+firefox环境搭建

java selenium+firefox环境搭建

已经成功搭建的版本关系:FireFox45+selenium3.141.59+ geckodriver 0.21.0

启动浏览器后空白页:浏览器版本太高

Firefox历史版本

http://ftp.mozilla.org/pub/firefox/releases/

geckodriver驱动版本

https://github.com/mozilla/geckodriver/releases

selenium  maven地址

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

配置代码

        WebDriver driver;
        //火狐安装位置
        System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
        //加载驱动
        System.setProperty("webdriver.firefox.marionette", "D:\\JavaPorject\\Crawler\\geckodriver.exe");
        //实例化一个对象Firefox
        driver = new FirefoxDriver();
        driver.get("https://www.baidu.com");

 

mac 搭建 python+selenium+firefox 爬虫

mac 搭建 python+selenium+firefox 爬虫

今天闲来无事看到 selenium 可以做自动化测试框架,就来试试手。
1、安装 pip
2、pip install –U selenium
完成之后就可以耍代码了,先举个栗子,测试一下:

from selenium import webdriver
import time

dr = webdriver.Firefox()
time.sleep(5)
print ''will close''
dr.quit()
print ''close''

出师不利啊,报错了:

selenium.common.exceptions.WebDriverException: Message: ''geckodriver'' executable needs to be in PATH.

原来是少了个驱动:geckodriver,链接给你,拿走不谢。
接着修改代码:

from selenium import webdriver
import time

dr = webdriver.Firefox(executable_path=''/Users/JYD/Downloads/geckodriver'')
time.sleep(5)
print ''will''
dr.quit()
print ''close''

大功告成,完美!

我们今天的关于使用Selenium JavaMac OSX将Firefox浏览器置于最前面的分享已经告一段落,感谢您的关注,如果您想了解更多关于centos 系统中安装使用selenium,firefox、Firefox更新后无法使用Selenium打开浏览器、java selenium+firefox环境搭建、mac 搭建 python+selenium+firefox 爬虫的相关信息,请在本站查询。

本文标签: