简体   繁体   中英

Webpage not opening in Firefox - Selenium Python

I am trying to run a webpage remotely using Selenium and then trigger a button click on it. I am able to successfully open Firefox, but the webpage is not loading, and Firefox closes automatically after sometime. (Tried google.com and other pages just as test, that too not loading). Can anyone suggest what to do here ?

OS - Ubuntu 14.04.1

Python - 2.7.6

Selenium - 3.3.0

Firefox - 39.0.3

Here is my python code

import urllib, urllib2, cookielib
from contextlib import closing
from selenium import webdriver
from selenium.webdriver import Firefox # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait

with closing(Firefox(executable_path="/usr/bin/firefox")) as driver:
    driver.implicitly_wait(10)
    driver.get("https://www.google.com")
    #driver.get("http://wsb.com/Assingment2/expcase16.php")
    button = driver.find_element_by_id('send')
    button.click()
    target_window = driver.window_handles[1]
    driver.switch_to_window(target_window)
    exploit_page_content = driver.page_source
    print "Exploit successful \n" + exploit_page_content

I suspect selenium is trying to use geckodriver, since that's the default. But it's only supported from version 48 of Firefox. See Jim's answer to a different question for more info . Try using the legacy Firefox driver, like so:

driver = Firefox(executable_path="/usr/bin/firefox", capabilities= {"marionette": False })

It seems that you're using conextlib.closing() and as per the documentation, you're basically calling the close() method on your object:

from contextlib import contextmanager

@contextmanager
def closing(thing):
    try:
        yield thing
    finally:
        thing.close()

And as per selenium documentation:

driver.close() – It closes the the browser window on which the focus is set.

As far as suggestions, it depends on what you want to do. If you want to continue processing the web page, then obviously extend your code. Or remove the context and call driver.close() explicitly when you are done. Again, it depends on what your task is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM