Running selenium (chromedriver) in a terminal window? - python

How can i run a python script using selenium (chromedriver) in an terminal window (without X forwarding).
The results/interactions with the website don't need to be shown. Par example filling a form scheduled by a cronjob.

you can use PyVirtualDisplay with Xvfb
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
# now Chrome will run in a virtual display.
browser = webdriver.Chrome()
browser.get('http://www.google.com')

Related

Chrome browser closes after running selenium chrome webdriver

I'm currently learning Selenium 4.0 and have set up a basic script that will click a button on Python's website. I'm using a Chrome webdriver. But whenever I run my code, a chrome window opens to the Python website and then closes immediately. How do I keep it open?
The browser version and the webdriver version are the same, and I've even tried the Edge webdriver and reinstalling Chrome. I've even tried downloading a webdriver to my local directory, but that doesn't work either. Here's my current script:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.get("https://www.python.org/")
print(driver.title)
submit = driver.find_element(By.ID, "submit")
submit.click()
After running, my terminal gives this message:
====== WebDriver manager ======
Current google-chrome version is 101.0.4951
Get LATEST chromedriver version for 101.0.4951 google-chrome
Driver [/Users/user1/.wdm/drivers/chromedriver/mac64_m1/101.0.4951.41/chromedriver] found in cache
Welcome to Python.org
Process finished with exit code 0
Well, it is the correct behavior as it does everything you told it to do correctly. Infact you're not recieving any errors. After having executed the code, Chrome Driver got killed because the Python app finishes its execution
If you want the Browser opened by the Driver to stay open use Chrome option and add detach
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

Python Selenium Chrome Not Running Headless

I'm currently writing a program in which I'm connecting to chrome. I would like to make this process without a new chrome window popping up every time. I know that --headless makes chrome run silently, but my current code is not working; it is still opening up chrome tabs.
Current Code:
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options = chrome_options)
What am I doing wrong or what do I need to add?
If you are using linux one alternate solution to run the browser in headless mode is to use pyvirtual display.
First install xvfb and pyvirtual display:
sudo apt-get install xvfb xserver-xephyr
sudo pip install pyvirtualdisplay
The code will be:
from pyvirtualdisplay import Display
import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Chrome()

Python-Selenium test script not working

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).

Schedule selenium to run on a remote server

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

Selenium Python script

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

Categories

Resources