For example some user launched his browser, then he launched my program. I need to interact with this browser (not necessarily Chrome).
Is there a solution for my problem in Selenium? Or in another way?
1) I saw the almost suitable way. It is to launch Chrome from Command Prompt by this command:
chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\ChromeProfile"
And then to attach launched browser in code like this:
# PYTHON Example
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "localhost:1234")
#Change chrome driver path accordingly
chrome_driver = "C:\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chrome_options)
But this is a manually launched browser
2) Or the second way at this link. But there is problem from the first try.
3) In this issue on GitHub I found this class, but it doesn't work for me. In the issue was written that it was tested only with Firefox (on ubuntu).
Is there a solution? Thanks in advance.
This is a very hacky solution considering Software development practices but here goes..
Once you initiate Google chrome manually or with a script, you can interact with that using PyAutoGUI module. We can move the mouse to a position on the screen, click and then type.
An example code after opening chrome :
import pyautogui, time
pyautogui.PAUSE = 2
# x and y is the mouse position on your screen
pyautogui.moveTo(x=410, y=79)
pyautogui.click()
# select all
pyautogui.hotkey('ctrl', 'a')
pyautogui.press(['delete'])
time.sleep(2)
pyautogui.typewrite("https://google.com")
pyautogui.press('enter')
Related
I am trying to build a TinderBot.
Both Chrome and FireFox ask for geo permission outside of code (the pop-up drops from the browser's address bar, so it's not inside the html and I cannot access it with .find_element)
I found some prompts on Chrome here: https://testingbot.com/support/selenium/permission-popups (didn't try it though, so not sure if they are up to date)
But I cannot find anything for FireFox.
I found this piece of code that disables javascipt
https://www.selenium.dev/documentation/webdriver/capabilities/firefox/
And I believe that I could build on it, but I cannot find how to set it so that it gives permission for geolocation.
Recently I though I found here a piece that at least might show me how to correctly pass '-hedless' argument but it won't open browser now.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.add_argument('-headless')
srvc = Service(GeckoDriverManager().install())
driver = webdriver.Firefox(options=opts, service=srvc)
So generally I have 2 problems here:
How do I make add_argument work? (I mean other cases turned out deprecated)
What arguments do I need to target to allow geolocation on launching browser with the bot?
Am I even on the right path? I cannot ask questions in relevant threads because of insufficient rating, so here I am.
To initiate Firefox browser in headless mode instead of using add_argument() you need to set the headless property to true as follows:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.headless = True
srvc = Service(GeckoDriverManager().install())
driver = webdriver.Firefox(options=opts, service=srvc)
You can find a relevant discussion in How to make Firefox headless programmatically in Selenium with Python?
I want to open a chrome window using the webbrowser module in Python. What I want to do next is to make the program resize the window. I've seen people do it with the selenium module, but it seemed way too complex for me since I have no experience with selenium. In addition, selenium webdriver doesn't work for me. Can someone help me resize a chrome window without the use of selenium?
I tried solving the problem with pyautogui as others suggested, but didn't find a way to solve my problem. I figured there was no easier way other than using selenium, so I decided to learn how to use it. I experimented with selenium and figured out how to resize and change the position of a chrome browser along with some other things. Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:\chromedriver.exe")
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.set_window_size(400, 600)
driver.set_window_position(300, 20, windowHandle ='current')
print(driver.get_window_size())
I want to create an application which will take a keyword as input and search that on youtube and then scrape the links and save them and in a Notepad file, and all of this works in background. I am familiar about BeautifulSoup library, and selenium but I want it to work in background unlike Selenium which works in front of us. I hope the question is clear, if not you may ask.
I am familiar about Selenium, but I want to automate the search in background.
from selenium import webdriver
driver=webdriver.Chrome("C:\\Users\\MyPC\\Downloads\\chromedriver_win32\\chromedriver.exe")
driver.set_page_load_timeout(10)
driver.get("http://www.youtube.com")
driver.find_element_by_name("search_query").send_keys("Selenium Tutorial")
driver.find_element_by_id("search-icon-legacy").click()
time.sleep(4)
driver.quit()
This code opens the browser and then performs the search, but I want everything to happen in background and fast without and delay.
You can run browser with option --headless and it will not display its window. It works for Firefox and Chrome.
Firefox
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
#options.headless = True
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)
driver.get('https://stackoverflow.com')
driver.save_screenshot('screenshot-firefox.png')
driver.close()
Chrome
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
#options.headless = True
options.add_argument('--headless')
#options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=options)
driver.get('https://stackoverflow.com')
driver.save_screenshot('screenshot-chrome.png')
driver.close()
There was webdriver PhantomJS which simulated headless web browser but it is not developed any more. This code still runs but it gives me empty page_sourceand empty file screenshot.png
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('https://stackoverflow.com')
print(driver.page_source)
driver.save_screenshot('screenshot.png')
driver.close()
On Linux you could use Xvfb to create fake/virtual monitor and program can use it to display its window. This way you don't see this window on your screen.
All this methods has to render page so it may not run faster.
To scrape faster you would have to analyze requests/responses from/to web browser and do the same with python module requests - but it is not ease. But this way program doesn't have to render page and run JavaScript so it will run a lot faster.
But then you may have another problem - if you make request too offen (too fast) then server can block you and you need proxy servers to have different IPs.
theres a workaround using pyvirtualdisplay also if you want to hide the selenium browser, dont forget to close the browse when you're done
i think webhosts can detect headless browsers, so u may get different results
stop loading the page after u get what you're looking for and/or close the browser after you save the source, insert javascript
I searched for this question,and I found an idea using driver.switch_to.window(),but it didn't work as expect:
from selenium import webdriver
driver1=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver1.get('https://www.google.com')
driver2=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver2.get('https://www.bing.com/')
driver1.switch_to.window(driver1.current_window_handle)
above code will first open a chrome window and go to google,then will open another chrome window and go to bing,then
driver1.switch_to.window(driver1.current_window_handle)
seems didn't work,the window showing bing still shows on top of the window showing google.
Anyone have any idea?I think
driver1.switch_to.window(driver1.current_window_handle)
may have some BUG.
As you have used two WebDriver instances as driver1 and driver2 respectively to openthe urls https://www.google.com (e.g. windowA) and https://www.bing.com/ (e.g. windowB) it is worth to mention that the function switch_to.window() is a WebDriver method. So, driver1 can control only windowA and driver2 can control only windowB.
For Selenium to interact with any of the Browsing Window, Selenium needs focus. So to iterate among the different Browsing Windows you can shift the focus to the different Browsing Window using JavascriptExecutor as follows :
Python:
driver1.execute_script("window.focus();")
driver2.execute_script("window.focus();")
Java:
((JavascriptExecutor) driver1).executeScript("window.focus();");
((JavascriptExecutor) driver2).executeScript("window.focus();");
I believe you have a different concept of "window" in driver.switch_to.window(). In chrome browser, it means "tab". It's not another chrome browser or browser window like what are you trying to do in your code.
If switch_to.window() what you really want, I'll give an example how to use it:
driver=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver.get('https://www.google.com')
# open a new tab with js
driver.execute_script("window.open('https://www.bing.com')")
driver.switch_to.window(driver.window_handles[-1])
# now your driver is pointed to the "tab" you just opened
When my script opens the chromedriver (driver = webdriver.Chrome()) in Windows 10, the window maximizes to full size on my monitor. To minimize disruption, I would prefer it was barely noticeable. The closest I can get is the following:
driver = webdriver.Chrome()
driver.set_window_size(1, 1)
This works well in that the window size is minimal. Unfortunately, it still first pops open to the full monitor and then readjusts to the preferred minimal size. Is there any way to preset the window size before opening up the chromedriver?
You can place driver.set_window_size(1,1) in the setup method for example:
class ExampleTestSuite(unittest.TestCase):
def setUp(self):
self.driver= webdriver.Chrome()
self.driver.set_window_size(1,1)
def test_example_test(self):
#test code here
What I think you really are looking for is running your tests headless. One way to do it this describe in this post: http://fgimian.github.io/blog/2014/04/06/headless-browser-testing-with-chrome-and-firefox/.
Another option is to use a cloud based testing service like Sauce Labs, or Browserstack.
I actually came across a pretty good alternative to #user3277225's answer. It turns out you can set options for the Chrome webdriver and then open it as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("window-size=1,1")
driver = webdriver.Chrome(chrome_options=chrome_options)