Python selenium webdriver remote wont opening new tab - python

i am having this issue where every time i do
driver.execute_script("arguments[0].click();", element)
or
driver.execute_script("window.open('_blank');")
browser blocks popup and prevents new tab from opening but when i use this
element.click()
It opens new tab without any issue so the problem is i am using webdriver.Remote for multilogin platform to automate some tasks so the execute_script method is very quick on the other hand element.click() takes forever.
What is the difference between them and for execute script new tab won't open i have tried doing this
options = webdriver.ChromeOptions()
options.set_capability("profile.default_content_settings.popups", 0)
driver = webdriver.Remote(link, options=options)
also added the same in experimental options as well as prefs but nothing works
thanks for your time any input is appreciated

Related

Timeout to a function Python on Windows

I use Selenium with Chrome WebDriver on mobile mode, I got an element and I need to click on it.
When I click, A pop-up is visible, which is good for me, but the click function is still going.
It's stuck and doesn't finish.
Also, when I click the element, there is no new screen on the browser, just an object visible, this is why the click button is not getting any response and doesn't finish.
I want to wrap this click function with a timeout to finish after a couple of seconds.
This must run on a Windows and I can not open a new process/thread for it, just finish the call function and continue with the code.
This is an idea of the code:
from selenium import webdriver
driver= webdriver.Chrome()
driver.get('https://webstie.com')
toggle_to_mobile() # THIS IS PRIVATE METHOD
element = driver.find_element_by_name("my_element")
element.click() # THIS IS STUCK
.
.
Thanks!

Firefox browser wont open maximized and wont switch tabs using Selenium

I'm opening Firefox browser through selenium(3.141.0) on python 3.9, and it will always start minimized, even though in code I passed function .maximise_window() after opening the browser. When its started this way, it wont execute code to maximise and it wont execute code to switch tabs, and it wont even do it if im trying to switch tabs manually with my mouse.
If I immediately click on the initiated firefox browser on my taskbar when the program is started, it will function normally, but it will open my browsers home tab, and execute my code in new tab. Thats why you may see in code a part that closes that first tab. When i dont do it and when it starts "faulty", the home tab wont be opened.
Im also using the lenght of tabs as an indicator if the browser initiated faulty, where I tried to put it into loop and make it restart till it opens with 2 tabs, but it just wont unless I manually click it.
The only solution so far I can think of is kinda "hacky"...using pyautogui to scan my taskbar after initializing browser and clicking it fast, but I dont really like the idea.
The code goes through my company data warehouse site and manipulates it to download data.
Update
Other hacky solution I found is starting 2 browsers. First won't work, but second will. Meaning that browser works normally IF there is another Firefox browser open at the time.
Snippets of code:
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
class DWH_browser:
def __init__(self):
self.browser = webdriver.Firefox()
self.browser.maximize_window()
self.browser.get("www.letskeepthecompanysiteasecret.org")
self.len_windows = len(self.browser.window_handles)
print(len(self.browser.window_handles))#this next part is used to close the extra tab when browser
#started normally and opens an extra tab
if len(self.browser.window_handles) == 2:
self.browser.switch_to.window(self.browser.window_handles[0])
self.browser.close()
self.browser.switch_to.window(self.browser.window_handles[0])
self.a = ActionChains(self.browser)
time.sleep(5)
DWH = DWH_browser()#and i initiate it in the code "normally"
#This was the other code I tried using to initate the browser and restart till its in 2 tabs, but not working
# issue_lenght = 1
# while issue_lenght == 1:
# DWH = DWH_browser()
# issue_lenght = DWH.len_windows
# if DWH.len_windows == 1:
# DWH.browser.quit()
# print("RESTARTING BROWSER")
Summarizing your issues:
Always start minimized.
Passed function maximise_window() after opening the browser.
Won't execute code to switch tabs.
To get rid of all these issues you need to ensure that:
Selenium is upgraded to current levels Version 4.4.0.
GeckoDriver is updated to current GeckoDriver v0.31.0 level.
Firefox Browser is updated to current firefox=103.0.2.
Additionally, you won't be needing self.browser.maximize_window() as firefox by default opens in a maximized mode.
Finally, trying to switch tabs manually with my mouse is a big no as the program execution may get interupted.

How to open a new window on click with python, selenium, pyvirtualdisplay?

I wrote a scraper in python which uses selenium, chrome and an added extension. The scraper works locally with the display visible but not when I try to run it in headless mode.
On the scraped website there is a button which automatically opens a new window (I cannot access it via URL in order to open it in a new tab).
click_button() # the button opens a new window with a confirmation button
driver.switch_to.window(driver.window_handles[1]) # switching to the confirmation window
click_confirm() # when confirm is clicked the window closes automatically
driver.switch_to.window(driver.window_handles[0]) # switching back to original window
Chrome doesn't work in headless when it has extensions installed so I'm forced to use pyvirtualdisplay.
The problem is that the Display of pyvirtualdisplay opens only one browser window, therefore the confirmation window never appears. How can I allow pyvirtualdisplay to open new windows and alternate between them?
Turned out that pyvirtualdisplay did open a window, however its size was 1px by 1px. Apparently setting up the webdriver window size the following way doesn't work properly with pyvirtualdisplay:
options = Options()
options.add_argument('--window-size="1980,1080"')
driver = webdriver.Chrome(executable_path=chrome_driver_path, options=options)
Setting it up like bellow solved my problem and the window was of a normal size:
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.set_window_size(1980,1080)

Initiating 'print' on google chrome using selenium

I'm having a hard time initiating the 'print' event on google chrome using selenium.
I've tried all the following both on OSX and Windows with no luck. For OSX I've replaced Keys.CONTROL with Keys.COMMAND/Keys.META.
driver = webdriver.Chrome()
driver.get("http://google.com")
# 1st try
actions = ActionChains(driver)
actions.move_to_element(driver.find_element_by_tag_name('body'))
actions.key_down(Keys.CONTROL).send_keys('p').key_up(Keys.CONTROL)
actions.perform()
# 2nd try
ActionChains(driver).key_down(Keys.CONTROL).send_keys('p').key_up(Keys.CONTROL).perform()
# 3rd try
ActionChains(driver).send_keys(Keys.CONTROL, "p").perform()
# 4th try
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + 'p')
None of the above did the trick. The only method that worked is driver.execute_script("window.print()"), but that is not the behaviour I'm looking for in this case.
Chrome driver version 80.0.3987.106.
Any ideas? Is there a way to initiate the 'print' event without the use of hotkeys?
A few words:
You should not be testing print dialog as it is a part of browser functionality and not related to your application.
If your usecase is to test the browser but not the web application you should be aware that sticking to key events is not the best option to proceed as if/when you come to the situation of tests execution in Selenium Grid or any form of Parallel Test Execution you may run intorace condition.
Solution
So the way to go is Window.print() function instead and you can use either of the following strategies:
Using execute_script():
driver.execute_script("window.print()")
Using execute_async_script():
driver.execute_async_script("window.print()")

How to avoid WebDriverException caused by Firefox updates

Several times I have faced this issue: I run my Python-Selenium script using Firefox, but instead of browser window, updates downloading pop-up appears and I get WebDriverException: "The browser appears to have exited ". Just after download completed browser window opens, but script is already stopped.. So how can I avoid script exit and force webdriver to wait until Firefox updates completion...
Disable auto update by passing custom FirefoxProfile when you open the
browser.
from selenium import webdriver
fp = webdriver.FirefoxProfile()
fp.set_preference('app.update.auto', False)
fp.set_preference('app.update.enabled', False)
browser = webdriver.Firefox(firefox_profile=fp)
Another solution is to disable autoupdate manually Options>Advanced>Update by "Never check for updates" radio btn. In my case its better idea because I have full control of compability between FF and WebDriver version then. Once I want to move to newer/newest version I do it via Help>About Firefox or if Im interested in specific version I can always download it here

Categories

Resources