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
Related
My code is very short. When I try it, the program opens web page but program closes immediately after.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome()
driver.get("https://google.com")
I believe the problem is coming from my environment, so I have not tried any changes to the code to resolve this.
You have to add the below chrome options:
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
driver.get("https://google.com")
I'm trying to webscrape images from the web using selenium. On Linux there do not seem to be any problems however when running on windows the program seems to exit on line 18.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
import os
import requests
import base64
def webscrape(query, max_count, q):
options = Options()
options.add_argument('-headless')
# Add a GH_token to the environment so there are no limitations on the number of requests.
os.environ['GH_TOKEN'] = "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"
gecko = GeckoDriverManager().install()
driver = webdriver.Firefox(service=Service(gecko), options=options)
driver.get("https://www.google.com/search?q=" + query + "&tbm=isch")
print("test")
the test print doesn't happen. But all print statements before the driver.get call do.
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
#Open browser
driver = webdriver.Chrome(ChromeDriverManager().install())
url='https://www.premierleague.com/players'
driver.get(url)
This the code I have. It only opens for a second and then closes again. Does anyone know why?
I am assuming the problem is with the chrome driver manager, but I can't figure out how to fix it.
You can use time.sleep(10) to keep the window open. I'm using 10 as an example for the number of seconds.
I am sucesfully able to load my chrome profile using the flags:
user-data-dir as well as profile-directory, yet once the profile is loaded and the chrome window is actually open, no webpage appears. It simply gets stuck on a blank screen.
When I remove the code for the profile it is actually able to open the webpage stored in the login-url variable.
Tried updating to latest version of chrome (94.0.4606.81) and I also used the exact steps listed here to ensure I have the right chrome driver version.
I also did the obvious like making sure there are not any instances of chrome running in the background.
Code is as follows:
import os
from os.path import exists
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
headless = False
login_url = "https://google.com)"
def startChrome():
global headless
try:
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
chrome_options.add_argument("----user-data-dir=C:/Users/ERIK/AppData/Local/Google/Chrome/User Data")
chrome_options.add_argument("--profile-directory=Profile 1")
global driver
driver = webdriver.Chrome(path+"/chromedriver.exe", options=chrome_options)
except:
print("Failed to start Chrome!")
input()
exit()
startChrome()
driver.get(login_url)
input()
The following successfully opens google.com for me.
Selenium Version 3.141.0
ChromeDriver Version 94.0.4606.61
Chrome Version 94.0.4606.71
from os import path
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
def getDriver(profile_directory, headless = False):
chrome_options = Options()
if headless:
chrome_options.add_argument("--headless")
userDataDir = path.expandvars(r"%LOCALAPPDATA%\Google\Chrome\User Data")
chrome_options.add_argument(f"--user-data-dir={userDataDir}")
chrome_options.add_argument(f"--profile-directory={profile_directory}")
return webdriver.Chrome("./chromedriver.exe", options=chrome_options)
driver = getDriver("Profile 2")
driver.get("https://google.com")
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