context_click() not working in headless chrome - Selenium - python

I am trying to right-click on a webpage using selenium context_click(). When I am doing this in normal mode, the context_click() is working, but in headless mode, it is not working. Doing this on chromedriver.
Here is the headless mode configuration,
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--start-maximized")
options.add_argument("--window-size=1088,1088")
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome('chromedriver',options=options)
And this is the code I am using to do context_click():
from selenium.webdriver import ActionChains
# action chain object creation
action = ActionChains(driver)
action.move_by_offset(200, 200)
# right click operation and then perform
action.context_click().perform()
#get screenshot
driver.get_screenshot_as_file("/content/screenshot3.png")

Related

Selenium does not open windows as maximized

I need to open the maximized page, but selenium does not work. It just opens the page usually.
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=r'/Users/chromedriver', options=options)
Do you mean --start-fullscreen?
You should try this options.add_argument("window-size=1920,1080")
Per this post How to maximize chrome browser in default when using selenium in python you can try chrome_options.add_argument("--start-maximized") and depending on your version of chromedriver, it's worth reading through this post: How to maximize the browser window in Selenium WebDriver (Selenium 2) using C#?
driver.maximize_window() also seems to be an option to try.
The -- shouldn't be there.
The correct syntax is:
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)

Selenium: Website always opens with selenium but then the site goes completely white immediately and keeps loading forever

I try to open the following site using selenium:
https://www.honestdoor.com/
Normally this works fine with every site with the following code:
(I am currently using google-chrome version 98.0.4758 - using ChromeDriverManager for downloading the version - see below in the code)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from fake_useragent import UserAgent
import time
if __name__ == '__main__':
ua = UserAgent()
userAgent = ua.random
options = Options()
# options.add_argument('--headless')
options.add_experimental_option ('excludeSwitches', ['enable-logging'])
options.add_argument("start-maximized")
options.add_argument('window-size=1920x1080')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument(f'user-agent={userAgent}')
srv=Service(ChromeDriverManager().install())
driver = webdriver.Chrome (service=srv, options=options)
waitWebDriver = WebDriverWait (driver, 10)
link = "https://www.honestdoor.com/"
# link = "https://www.bcassessment.ca/"
# driver.minimize_window() # optional
driver.get (link)
time.sleep(1000)
The site opens with selenium as allways but then the site goes immediately complete white and is still loading forever with the cicle going around in the top left corner (I can only kill the chrome-task in the task manager).
When I open the site in normal chrome or incognito chrome everything works fine - it seem to only crash when I open it with selenium. With other sites (like https://www.bcassessment.ca/ I have no problems at all and the open with selenium as allways)
Why is this not working for this particular website?
Not that super clear about the exact issue you are facing while loading the website. However I was able to load the website using the following code block:
Code Block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.honestdoor.com/")
Browser Snapshot:

Specific sites doesn't render using Selenium (python)

I've been using selenium with python on both Chrome and Firefox. This specific website stays blank on both browsers when I try to run it with selenium - I'd appreciate any help. Here's my code for chrome:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option("excludeSwitches",["enable-automation"])
path = r'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(executable_path=path, chrome_options=options)
driver.get('https://main.knesset.gov.il/Activity/committees/pages/allcommitteesagenda.aspx')
Add the argument --disable-blink-features=AutomationControlled
Code Block:
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(executable_path=r'C:\BrowserDrivers\chromedriver.exe', options=options)
driver.get("https://main.knesset.gov.il/Activity/committees/pages/allcommitteesagenda.aspx")
Browser Snapshot:
References
You can find a couple of relevant detailed discussion in:
How to evade blocking by Walmart using Selenium Webdriver
Selenium can't open a second page

Selenium chromedriver how disable translation bar in headless mode

When opening some web pages in Selenium using Chrome driver I found that chrome shows the translation bar in headless mode, but it doesn't show it when headless is disabled.
Example (headless enabled)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
chrome_options = ChromeOptions()
chrome_options.headless = True
chrome_options.page_load_strategy = 'normal'
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--lang=en')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://blog.bellostes.com/?attachment_id=5392')
driver.save_screenshot('headless_true.png')
driver.quit()
Result:
Example (headless disabled)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
chrome_options = ChromeOptions()
chrome_options.headless = False
chrome_options.page_load_strategy = 'normal'
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--lang=en')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://blog.bellostes.com/?attachment_id=5392')
driver.save_screenshot('headless_false.png')
driver.quit()
Result:
What I have already tried:
chrome_options.add_argument('--lang=en')
chrome_options.add_argument('--disable-features=TranslateUI')
chrome_options.add_argument('--disable-translate') # Not supported any more on Chrome
No difference. Is any I can do to get rid of the translation bar in headless mode?

Is there a way to hide the browser while running selenium in Python?

I am working on a project with selenium to scrape the data, but I don't want the browser to open and pop up. I just wanted to hide the browser and also not to display it in the taskbar also...
Some also suggested to use phantomJS but I didn't get them. What to do now ...
If you're using Chrome you can just set the headless argument like so:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
driver_exe = 'chromedriver'
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(driver_exe, options=options)
For chrome you could pass in the --headless parameter.
Alternatively you could let selenium work on a virtual display like this:
from selenium import webdriver
from xvfbwrapper import Xvfb
display = Xvfb()
display.start()
driver = webdriver.Chrome()
driver.get('http://www.stackoverflow.com')
print(driver.title)
driver.quit()
display.stop()
The latter has worked for me quite well.
To hide the browser while executing tests using Selenium's python you can use the minimize_window() method which eventually minimizes/pushes the Chrome Browsing Context effectively to the background using the following solution:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
driver.minimize_window()
Alternative
As an alternative you can use the headless attribute to configure ChromeDriver to initiate google-chrome browser in Headless mode using Selenium and you can find a couple of relevant discussions in:
How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
If you're using Firefox, try this:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
driver_exe = 'path/to/firefoxdriver'
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(driver_exe, options=options)
similar to what #Meshi answered in case of Chrome
if you want to hide chrome or selenium driver there is a library pyautogui
import pyautogui
window = [ x for x in pyautogui.getAllWindows()]
by this, you are getting all window title
now you need to find your window
for i in window:
if 'Google Chrome' in i.title:
i.hide()
or you can play with your driver title also

Categories