I am trying to set up python selenium to work on my WSL 2 (Kali).
I have followed along with this article https://www.gregbrisebois.com/posts/chromedriver-in-wsl2/
Running "google-chrome" in terminal returns a working browser.
Trying to run this test script results in a browser window, but nothing loads into the window and no code after driver = webdriver.Chrome() runs.
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
print('starting')
driver = webdriver.Chrome()
driver.get('https://www.google.com')
print('Worked')
driver.close()
This is the browser the script returns but never stops loading
Related
I try to open browser using Selenium in Python and after the browser opens, it exits from it, I tried several ways to write my code but every possible way works this way.
Thank you in advance for help
`from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.get("https://amazon.com")`
I expected the browser to open amazon.com and stay like this until I close or the programme close it.
Actual result - when the browser loads the website, it exists from itself.
Use driver.close() function after getting result ;)
I am running a basic python program to open the Chrome Window but as soon as the code executes, the window is there for a sec and then it closes immediately.
from selenium import webdriver
import time
browser = webdriver.Chrome(executable_path=r"C:\APIR\chromedriver.exe")
browser.maximize_window()
browser.get("https://www.google.com")
Chromedriver version: 91.0.4472.101
Chrome Version: 91.0.4472.164
Any help would be appreciated.
Thank you
It closes because the program ends.
You can:
Wait with time.sleep, for example time.sleep(10) to keep the browser open for 10 seconds after everything is done
Have the user press enter with input()
Or detect when the browser is closed. Many ways to do that.
Example: https://stackoverflow.com/a/52000037/8997916
You could also catch the BrowserUnreachable exception in a loop with a small delay
For Edge Browser
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager
options = webdriver.EdgeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Edge(options=options, service=Service(EdgeChromiumDriverManager().install()))
driver.maximize_window()
driver.get('https://stackoverflow.com/questions/68543285/chrome-browser-closes-immediately-after-loading-from-selenium')
For Chrome Browser
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options, service=Service('./path/to/chromedriver'))
driver.maximize_window()
driver.get('https://stackoverflow.com/questions/68543285/chrome-browser-closes-immediately-after-loading-from-selenium')
I have a javascript which I want to put in console when page loaded but i am unable to open console using selenium in python I can access devtoos using this --auto-open-devtools-for-tabs but not able to open console.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
import os
import time
from urllib.parse import quote_plus
driverPath = "chromedriver.exe"
dataPath = "whatsapp-assistant-bot-master/Data"
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=" + dataPath)
options.add_argument("--auto-open-console-for-tabs=" + dataPath)
driver = webdriver.Chrome(options=options, executable_path=driverPath)
driver.get('http://google.com')
print('Please Scan the QR Code and press enter')
driver.find_element_by_id("gsr").send_keys(Keys.F12)
input()
driver.quit()
Answered here, you don't have to open the console, you can run your js code through selenium
Running javascript in Selenium using Python
I’m working to make web crawler with python by using selenium
Here, I successfully got contents by using chromedriver, but problem occurred when I tried to make
Headless access crawling through PhantomJS. find_element_by_id, or find_element_by_name did not work
Is there any difference between these? Actually I am trying to make this as headless because I want to run this
Code in ubuntu server as a batch job without GUI support.
My script is like as below.
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import re
#driver = webdriver.PhantomJS('/Users/user/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs')
#driver = webdriver.Chrome('/Users/user/Downloads/chromedriver')
driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550)
driver.get(url)
driver.implicitly_wait(3)
#here I tried two different find_tag things but both didn’t work
user = driver.find_element(by=By.NAME,value="user:email")
password = driver.find_element_by_id('user_password')
I am using Python Selenium to open a Firefox browser and go to a URL. The function I am using to do this is...
def openurl_function():
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.example.com')
When I run the function it always opens a new instance of FireFox, is there a way to have it to just open using the same browser instance?
Currently if I run the function 10 times then I get 10 FireFox browsers open.
Just keep reusing the same driver. You are creating a new browser, every time you call
driver = webdriver.Firefox()
Also, because you never quit() on your driver, you will probably have all the browsers stay open as orphans because you deleted the handle to them when you created a new browser.