This question already has answers here:
How do I make Chrome Headless after I login manually
(2 answers)
Closed 4 years ago.
Is there any way to switch Chrome webdriver from headless mode to window mode?
One thing that came to my mind is to 'switch' existing web driver to non-headless mode.
Another idea: to create new instance of webdriver (this time non-headless) with some sort of 'state' from old one so the user operations can be executed. I don't know how to do or if it is possible though.
import os
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException,
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(
executable_path=os.path.join(os.getcwd(), 'chromedriver'),
chrome_options=options,
)
driver.get('https://website.com')
try:
driver.find_element_by_xpath('//h1').click()
except NoSuchElementException:
print('You have to click it manually')
# here I need Chrome browser
# to be opened so that I can click a link
print('The name of this thing is: ', end='')
print(driver.find_element_by_xpath("//h1[#class='name']").text)
If you need to open a new tab
driver.execute_script("window.open()")
If you need to switch to this new one
driver.switch_to.window(self.driver.window_handles[1])
Then you get the page
driver.get('https://website.com')
and the end you can close it (the new one)
driver.close()
and you back to the first driver
switch_to.window(driver.window_handles[0])
Related
This question already has an answer here:
How can I switch from headless mode to normal mode using Google Chrome and Selenium?
(1 answer)
Closed 5 months ago.
Hello so i have been trying alot but i think it's impossible i need to run chrome driver using python selenium with headless on but at some point i want to turn off headless mode (in the middle of debugging for example) so i can solve the captcha or do something .. is it possible ?
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import chromedriver_autoinstaller
link = "https://www.google.com/recaptcha/api2/demo"
WINDOW_SIZE = "1920,1080"
try :
chromedriver_autoinstaller.install()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--log-level=3")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
driver = webdriver.Chrome(chrome_options=chrome_options)
wait = WebDriverWait(driver, 10)
driver.maximize_window()
driver.get(link)
#i want to turn off headless here so i can solve the captcha for example
print("Cpathca Solved")
#Then turn it back on again here
except :
pass
No, you can't.
webdriver object is created with specified (or default) parameters that can't be changed during the session.
i am trying to control my browser using python, what I need is I give commands in terminal that should work on the browser like opening and searching for something(like scorling the bowser) and closing the browser
currently I am done with opening the browser and closing
yes, you can by using python selenium driver.
This is simple code to test. Also don't forget to download selenium driver.
https://chromedriver.chromium.org/downloads
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.python.org")
print(driver.title)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
driver.close()
Read docs in link
This is achievable (at least in POSIX systems like Linux or BSD) by using pipes.
This question already has answers here:
How to make Selenium not wait till full page load, which has a slow script?
(2 answers)
Closed 3 years ago.
I have built a scraper in python 3.6 using selenium and scrapinghub crawlera. I am trying to fetch this car and download its photos. https://www.cars.com/vehicledetail/detail/800885995/overview/ but the page just keep loading for long periods of time. What I am trying to figure out is how can I stop the browser from continuously loading after 4 mins.
I have tried both explicit and implicit wait and none has worked.
driver = webdriver.Chrome('/usr/bin/chromedriver',
desired_capabilities=capabilities,
options=chrome_options)
driver.implicitly_wait(180)
driver.get(url)
You need to set the max waiting time for loading with driver.set_page_load_timeout().
In case the page passes its loading time, the browser will throw a TimeoutException. All you need to do is to take care of it
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
driver = webdriver.Chrome('/usr/bin/chromedriver',
desired_capabilities=capabilities,
options=chrome_options)
driver.set_page_load_timeout(time_to_wait)
try:
driver.get(url)
except TimeoutException:
# Do what you need here
This question already has answers here:
Selenium: Point towards default Chrome session
(2 answers)
How to open a Chrome Profile through --user-data-dir argument of Selenium
(3 answers)
How to use Chrome Profile in Selenium Webdriver Python 3
(12 answers)
Closed 3 years ago.
So thats my code, I want to open chrome with my default profile information in selenium. but its doesn't load and crashes when I click on profile icon in chrome
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/Users/Library/Application Support/Google/Chrome/Default/Default")
driver = webdriver.Chrome(executable_path='/Users/Desktop/supbot/chromedriver', options=options, service_log_path="/tmp/log")
driver.get('google.com')
You should use full path to the driver file like:
C:\pathtodriver\chromedriver.exe
Also, avoid using Capabilities, try this approach:
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get('google.com')
I want to open quite a few URLs in different tabs using Selenium WebDriver & Python.
I am not sure what is going wrong:
driver = webdriver.Chrome()
driver.get(url1)
time.sleep(5)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t')
url2 = 'https://www.google.com'
driver.get(item2)
I looked up tutorials and it seems to me as though this code should do what I want. What actually happens is the browser opens, url1 opens as it should, a new tab opens as it should but url2 then loads in the original tab instead of the new one (even though the new tab appears to be the active one).
(I am using Chrome because when using Firefox I can't get it to load any URLs at all. Firefox opens but does not get the url requested. I have tried to find a solution to this but to no avail.)
Is there anything I can change in my code to get the new URL to open in the new tab?
Thanks for your help!
Here is a simple way, platform independent:
Code:
driver.execute_script("window.open('http://google.com', 'new_window')")
Switching back to the original tab:
Code:
driver.switch_to_window(driver.window_handles[0])
Checking the current title to be sure you are on the right page:
Code:
driver.title
For everything else, have fun!
There is a bug in ChromeDriver that prevents ctrl/command+T from working:
I canĀ“t open new tab in ChromeDriver
What you can do, as a workaround, is to open a link in a new tab and then switch to a new window using the switch_to.window(). Working sample:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://www.google.com")
# open a link in a new window
actions = ActionChains(driver)
about = driver.find_element_by_link_text('About')
actions.key_down(Keys.CONTROL).click(about).key_up(Keys.CONTROL).perform()
driver.switch_to.window(driver.window_handles[-1])
driver.get("https://stackoverflow.com")
Now the last driver.get() would be performed in a newly opened tab.
An alternative way to open a new window is to use JavaScript and the window handler to switch between them.
driver = webdriver.Chrome()
# Open a new window
# This does not change focus to the new window for the driver.
driver.execute_script("window.open('');")
# Switch to the new window
driver.switch_to.window(driver.window_handles[1])
driver.get("http://stackoverflow.com")
# close the active tab
driver.close()
# Switch back to the first tab
driver.switch_to.window(driver.window_handles[0])
driver.get("http://google.se")
# Close the only tab, will also close the browser.
driver.close()
If you look at your browser while you're executing it will look like the new window has focus, but to the webdriver, it doesn't. Don't be fooled by the visual. Also remember to select a new window handler when you close a tab as it will set the driver.current_window_handle to
selenium.common.exceptions.NoSuchWindowException:
Message: no such window: target window already closed from unknown error: web view not found
(Session info: chrome=<Your version of chrome>)
(Driver info: chromedriver=<Your chrome driver version> (<string of numbers>),platform=<Your OS>)
on .close() and it will throw that error if you try to do stuff with the driver at that stage.
you need to maximize your chrome for this
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.google.com/")
e = driver.find_element_by_tag_name("body")
ActionChains(driver).key_down(Keys.CONTROL).click(e).send_keys("k").key_up(Keys.CONTROL).perform()
here key_down(Keys.CONTROL) will hold down ctrl key, to get focus on page i am clicking body of the page, then click k