selenium + python switch_to_window - python

I'm writing a scraper that requires selecting a link to open a new window. To activate the window and check that it is activated, I use :
driver.switch_to_window(driver.window_handles[1])
driver.title
print title
It worked once and then never again. I did some other check to make certain it was recognizing the windows existence and it does, yet it will not switch:
print len(driver.window_handles)
print driver.window_handles
I'm using the following website:
chromedriver = 'C:\Python27\drivers\chromedriver'
driver = webdriver.Chrome(chromedriver)
driver.get("https://ccrecordse.tarrantcountytx.gov/RealEstate/SearchEntry.aspx")
The program enters dates + lease documents, moves to the next page, clicks the document icons to open the new window, but will not switch to the new window. I cannot figure out why.
Thanks for your help and let me know if I need to provide more information in my question!

I opened the window by using get_element_by_id.click(). But it seems I was capturing the change incorrectly, and if I print driver.title, it shows the correct handler.
Thank you!

Related

How to interact with a separate pop-up window/website with Selenium / Chrome Driver in Python?

I am trying to automate a process with Selenium, and am having troubles figuring out how to switch between open windows while the program is running.
After clicking on the button, it opens another website that has a separate url, which is unique each time it is opened. I need to switch Selenium from interacting with the original website to this new popup within the browser, caused by the original website. The new window shows that it is also controlled by Chromedriver with the bit at the top that says "Chrome is being controlled by automated test software." Additionally, the actual website opened will be the same, just the fine print after the '.com/' is different.
How would I go about doing this? Also, how would I switch back? (If this is even possible)
For example:
driver=webdriver.Chrome(service=s)
driver.get("https://originalwebsite.com/")
driver.find_element(By.XPATH, 'buttons-xpath').click()
# (popup opens up now)
# *switch to popup website here*
driver.find_element(By.XPATH, 'button-on-new-website-xpath').click()
driver.find_element(By.XPATH, 'second-button-on-new-website-xpath').click()
# *popup website closes*
# switch back to original website / window
Thanks!
I have tried to use driver.navigate in a variety of ways but generally have no clue what I am doing. Thanks again!
The comment from ALex Break led to the answer.
All I had to do was:
handles = driver.window_handles
driver.switch_to.window(handles[x])
#handles[x] is the index of the list handles that has the handle I want to switch
#to stored in it

python selenium . how to switch back to previous window/tab after a download opens up a new tab

The problem is when I download a file through a click(), it opens a new tab/window to initiate the download, then the browser automatically closes the tab/window. However, when I want to access the previous page in which the download link was, I get the error "invalid session id".
I get the error "No such window exception" when using Safari for the automation.
If anyone knows how to deal with this issue I would appreciate all the help I could get. thank you all!
my code is below and the error comes after trying to click file_dl2
attachments = browser.find_element_by_id('sAttachments')
attachments.click()
time.sleep(2)
files = browser.find_element_by_xpath('//*[#id="FileListFI"]/div[1]')
files.click()
file_dl = browser.find_element_by_xpath('//*[#id="ctl00_chpDialog_hrefFileLink"]/img')
file_dl.click()
browser.implicitly_wait(10)
file_dl2 = browser.find_element_by_xpath('//*[#id="ctl01_chpDialog_hrefFileLink"]/img')
file_dl2.click()
....
driver.get("<web-site>")
sleep(1)
....
This fetches the original/previous website is fetched (add this code where you have click() action in role ).
You can append new set of instruction after sleep() function and wrok further.

{Python, Selenium} How can I detect browser windows that I'm already using and create a new tab inside said browser window?

I am currently working on a auto log-on for a website, but I found that selenium opens a new browser window each time I run the script. I want to make it so that it opens a new tab in the browser window I am already using. Selenium opens a new browser window, like so:
However, I want it to detect a window I am already using, and place a new tab there, like so:
Can we do this with Selenium for Python or do I need another language?
Sorry if my question is a bit stupid.
Thanks!
There are no stupid questions.
You need to find the reason why your selenium is opening new browser each time you log in.
It is probably because you are initialize webdriver each time you run that method.
Or maybe you use some hooks or tags which are triggering creation of new webdriver.
The code form other user from this site:
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
windows = driver.window_handles
time.sleep(3)
driver.switch_to.window(windows[1])

Fails to perform keys down and hit enter in context menu python selenium

I want to login to a site. Right-click on one of the links and open in New Tab or New Window.
I have searched earlier here and googled around before posting it here. May be I am doing it wrong
button=browser.find_element_by_link_text('Menus');
action=ActionChains(browser)
action.context_click(button).perform() #--> Till here working fine, Right clicks on Menu
action.send_keys(Keys.ARROW_DOWN+Keys.ARROW_DOWN+Keys.ENTER).perform() #--> Not Working
I wouldn't go into that direction as performing context menu clicks will bite you back when you'll be running your Selenium tests in Parallel Mode
Instead of opening context menu and clicking I would rather recommend:
Extract href attribute from the link
Use Window.open() JavaScript function to open the link in the new tab
Use Explicit Wait to wail until number of windows becomes 2
Switch the context to the new tab
Example code:
button = browser.find_element_by_link_text('Menus')
href = button.get_attribute("href")
browser.execute_script("window.open('" + href + "')")
WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))
browser.switch_to.window(browser.window_handles[1])

Python, Selenium Webdriver: The element is not visible for Firefox.

I'm trying to iteract with a webelement on a page that has edit link that opens a popup. In opened popup I have simple input field and Apply/Cancel buttons. In my script I do the following to enter some text to the input field:
def enter_text(self, text, action):
if self.is_element_present(self._input_locator):
self.selenium.find_element(*self._input_locator).send_keys(text)
if action == 'Apply':
self.selenium.find_element(*self._apply_button_locator).click()
elif action == 'Cancel':
self.selenium.find_element(*self._cancel_button_locator).click()
When I run my script in Chrome - everything works fine, all webelements are found and input text is entered to the field. But when I run exact same script in Firefox - it opens popup window (which means it became visible for Webdriver) with input field and 2 buttons but the text is not getting entered to the field which causes error:
ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with'
Why can this happen if the popup is actually opened (and I can see it) but Webdriver tells that it is not visible? Also, I put several sleeps just to get sure that popup loaded and then the text is entered but it did not help.
Any help will be appreciated.
Two ideas:
1: make sure your HTML is valid, eg by running it thru the w3c validator. broken html is a common cause of different behaviours in different browsers.
2: have you definitely switched selenium over to the popup, using, eg:
for handle in self.selenium.window_handles:
# identify popup window's handle somehow
self.selenium.switch_to_window(handle)
As an aside, if you haven't come across selenium.implicitly_wait(3), it's dead useful as a way of avoiding time.sleeps and wait-for constructs..
Witch Firefox version and Webdriver version are you using?
Please try the following:
Update Webdriver to the last version
If you are using firefox 17-18, downgrade it to lower version (I guess FF12 will work)
Please try that and tell me what happens,
Regards

Categories

Resources