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())
Related
I tried to click on chrome browser while using ActionChains.
from selenium.webdriver import ActionChains
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(URL)
link = driver.find_element_by_id('link')
action = ActionChains(driver)
action.double_click(advanced_options_link).perform()
But what is more?!
I want to see mouse cursor on browser to monitor what is exactly happening.
*** Please help ***
I needed to use a function like this in the past. as far as Iv'e researched I learned that it's not possible with selenium. you can hover on elements or even move the cursor to a specific location on the scree, but you can't see it moving because it's not really moving the cursor, selenium dose not have control over the cursor in that fashion.
You can try to research this, you might be able to use something outside selenium to control the actual cursor of the computer. but, even if it's possible it will still be very hard to get it to work reliably because you don't have the control on the website that you have with selenium so everything becomes manual, therefore hard and keen to generate errors.
Does it work if you try using the move_to_element() from ActionChains? something like this;
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get(URL)
link = driver.find_element_by_id('link')
hover = ActionChains(driver).move_to_element(link)
hover.perform()
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
While scraping a page using selenium webdriver, there is a "pop up" that appears .
On Opening the page, http://www.fanatics.com/search/red%20shoes - I see a popup window with xpath '//*[#id="mm_DesktopInterstitialImage"]' - but I don't want to be using the xpath to close this alert, and have something genric that can dismiss/close the alert.
Here's what I tried so far -:
from selenium import webdriver
import os
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
chromedriver = "/usr/local/CHROMEDRIVER"
desired_capabilities=DesiredCapabilities.CHROME
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver,desired_capabilities=desired_capabilities)
url='http://www.fanatics.com/search/red%20shoes'
driver.get(url)
#driver.set_page_load_timeout(300)
driver.implicitly_wait(60)
alert = driver.switch_to_alert()
alert.dismiss
handle=driver.window_handles
print handle
#[u'CDwindow-B1E9C127-719D-ACAA-19DE-1A6FA265A4FF']
From what I understand from related examples, folks usually switch window handles, but my handle has a single element.i.e, driver.switch_to_window(driver.window_handles[1]) then driver.close() and finally shift again, using driver.switch_to_window(driver.window_handles[1]) I also used implicit wait, since I was not sure, if the alert window was being read at-all - but that did not work either. I do not wnat to hard-code for the xpath,if that is possible.
What am I doing wrong ?
Related, but does't work for me : Selenium python how to close a pop up window?
As I can see, it's not an alert box!!. It is just a Simple Pop-up that appears when you are entering the page and it is present in main window itself(no need of switching and closing it too). Use the below code to close it.
driver.find_element_by_xpath('//div[contains(#class,"ui-dialog") and #aria-describedby="dialogContent2"]//button[#title="Close"]').click()
It works perfectly, give it a try ;)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--disable-notifications")
browser = webdriver.Chrome('C:\Python34\Lib\site-packages\selenium\webdriver\chromedriver.exe', chrome_options=options)
The popup you are trying to close is not a browser alert but a DOM popup. A browser alert is a dialog box that the browser creates at the OS level. (It is appears as an additional window on your desktop.) You can use .switch_to_alert() to close these alerts. A DOM popup is just an HTML element that happens to be used the the page as if it were a dialog box. This popup has existence only in your browser. (The OS does not know about it.) You cannot use .switch_to_alert() to close DOM popups.
To get this popup and close it, you have to inspect the DOM (like you've started doing), find the DOM element that represents the close button and have Selenium click it.
I ran into a similar problem, except that our pop-up's closebox webelement had a dynamic id tag, making it difficult to latch onto. We ended up solving it by using By.className to find it. In the code below, I use a List because I was also looking to see how many elements I was dealing with, but that's not required. Also, the production code has additional handling in case window closebox elements were found.
List<WebElement> closeboxes = driver.findElements(By.className("v-window-closebox"));
for (WebElement we : closeboxes) {
we.click();
}
You can try the following:
from selenium.webdriver import ActionChains, Chrome
driver = Chrome('/usr/local/CHROMEDRIVER')
ActionChains(driver).move_to_element_with_offset(
driver.find_element_by_xpath('//html'), 0, 2338
).click().perform()
This will click an area outside of the popup thus closing it. ðŸ¤
Reading here, there apparently used to be a RenderedWebElement class with a hover method. It, however, was exclusively made for Java (I have searched the Python bindings documentation to no avail) and has since been deprecated for Java.
A hover can't be performed using action_chains nor by using a WebElement object either.
Any ideas as to how to do this for Python? I have been here but it uses RenderedWebElement and hence doesn't help too much.
I am using: Python 2.7, Windows Vista, Selenium 2, Python Bindings
EDIT: There is a method mouse_over for a selenium.selenium.selenium object but I cannot figure a way to create an instance without having the stand-alone server running already.
EDIT Please go through the comments of the reply marked as answer just in-case you have misconceptions like I did !
To do a hover you need to use the move_to_element method.
Here is an example
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
firefox = webdriver.Firefox()
firefox.get('http://foo.bar')
element_to_hover_over = firefox.find_element_by_id("baz")
hover = ActionChains(firefox).move_to_element(element_to_hover_over)
hover.perform()
#AutomatedTester have given the community a great solution!
Below is how I used it.
I used signal to properly quit PhantomJS since it sometimes hangs in the current process.
I prefer to use find_element_by_xpath since xpath can be easily found in chrome.
Here's how:
Right click -> Inspect -> Right click -> Copy -> CopyXpath
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import signal
browser = webdriver.PhantomJS()
browser.implicitly_wait(3)
def hover(browser, xpath):
element_to_hover_over = browser.find_element_by_xpath(xpath)
hover = ActionChains(browser).move_to_element(element_to_hover_over)
hover.perform()
browser.service.process.send_signal(signal.SIGTERM) # kill the specific phantomjs child proc
browser.quit()