I am running following code in PyCharm
from selenium import webdriver
driver = webdriver.Firefox
Scripts executes successfully with following message in console
C:\Python34\python.exe C:/Users/dev/PycharmProjects/PYLearn/firs_selenium_script.py
Process finished with exit code 0
But Firefox Browser is not opening, I have selenium installed using pip, Do I need additional setting to make this work.
Thanks,
To launch the browser your code should look like:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://your-url')
After the test you will want the close the browser, so add:
self.browser.quit()
Hope this solve your problem.
More info: http://selenium-python.readthedocs.org/getting-started.html
Go to Python Console at the bottom and run your commands and check the error
You can try this:
from selenium import webdriver
browser = webdriver.Chrome()
Browser should be opened , anyways check whether the browser started running or not.
Once, your browser fully loaded, your command will move to next line
Related
As soon as I run my program, google opens (so far so good) but this window closes immediately after the program has run. I already installed the chromdriver (also matches the version of the search engine) and put it in the script folder in Python. Can someone help me?
Here is my Code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://google.com")
You need to specify an excecutable path for a chrome driver to be able to open a browser.
download a driver based on your browser in your case it's Chrome from here: https://chromedriver.chromium.org/downloads
then driver = webdriver.Chrome(here you have to write the path of the driver),you can put it wherever you want.
I'm trying to make an app that blocks some acces to certain websites, now i'm stuck thinking how to check the current url. I've tried selenium, but that doesn't work when you change tabs, so i had to try something else. I've been thinking about a chrome addon that checks current url and sends it to my python code, but i don't know how to do it without making any additional server. Any help appreciated.
You can use selenium and web drive manager for this
Install webdrive manager first using
pip install webdrive-manager
Then input the following code
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
print (driver.current_url)
I am trying to automation some action on a website using Python and Selenium, this is the sample code I am trying to run, from the Mozilla website for running the Firefox webdriver
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
caps["binary"] = "/usr/bin/firefox-aurora"
driver = webdriver.Firefox(capabilities=caps)
driver.get("https://www.google.com")
driver.quit()
When running this code, the Firefox instance opens normally, and I can even use the instance as if I just opened Firefox normally, but the code 'stops' executing at the driver = webdriver.Firefox(capabilities=caps) line, I tried debugging the code with no luck, the whole execution seems to just stop at this line, and nothing after it is reached!
I am running Python3.5, Selenium version 2.53.6, I have the 'Wires' executable at the /usr/local/bin which is in the environment's PATH, I also have Firefox Aurora version 49.0a2 running on ArchLinux.
Thanks in advance.
[Update:]
I managed to get it to work after all using Firefox 46 (the normal version).
I need to run selenium on a remote server since the server doesn't have a screen I'm using PhantomJS as a web driver. this is the code I Have:
import time
import os,sys
reload(sys)
sys.setdefaultencoding('utf8')
import re
from selenium import webdriver
url = 'https://wiki.python.org/moin/HowTo/Sorting'
driver_1 = webdriver.PhantomJS()
driver_1.get(url)
content = driver_1.page_source
On my computer, it works fine, but after that, I'll open other pages so I need the code keep running for a while.
I'm running the script from the terminal as:
python my_script.py
And the script will execute on the same terminal from where I'm accessing the server so either I won't be able to disconnect or if a loss the connection the script will stop, how can I handle this issue?
You do not need a read GUI on the server in order to run Selenium with any browser. Selenium can easily run on any server with real browsers like Chrome or Firefox.
Here is some code that should help you make it working:
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Chrome() # Or Firefox()
If you want your script to continue executing even if you lose connection to the server, you can easily do this with terminal software like Tmux or screen
I am wondering if there is a way within py2app to include the Firefox browser, or if there is a way for Selenium to use Firefox without having to install Firefox on the host machine.
I have created an app with py2app that uses Selenium, however, I have Firefox installed on my machine, but not everyone that will receive the app will have Firefox installed. I am looking for a way to either include Firefox in the distribution or go around this.
Script will not run if Firefox is not preinstalled.
You can test your script with other browser, for example Chrome. If it works on Chrome also, then you can edit script like this:
from selenium.common.exceptions import WebDriverException
try:
driver = webdriver.Firefox()
except WebDriverException:
driver = webdriver.Chrome()
You can add same for few more browsers (IE, Opera, Safari...) to be sure that script will run on users machine