Is there anybody who used ChromeDriver to open new tabs in Chrome?
With this code I can open my index.html in Chrome:
driver = webdriver.Chrome("/usr/bin/chromedriver")
driver.get("localhost:3000")
And then I want to open localhost:3000/cloudPeer.html in a new tab:
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + 't')
driver.get("localhost:3000/cloudPeer.html")
This works well in Firefox, but in Chrome, it can't open a new tab, it just refresh the first page, and open the new page, the first page is now lost.
I want to open these two pages at the same time and do some communication tests between them.
This works for chrome-
driver.execute_script("window.open('https://www.google.com');")
Related
I am trying to get the URL of the newly opened tab so I can work on it and find elements in that tab. How can one get a URL of a new opened tab that was opened by the website on click of a button using selenium in python.
In general Selenium automation, we first switch to newly opened tab and then we can get the URL and we can then interact with the element.
for switching to new tab, you can use the below code:
original_win_handle = driver.current_window_handle
# code to click on the first page and now expecting a new tab to be opened
all_win_handles = driver.window_handles
driver.switch_to.window(all_win_handles[1])
and now you've switched it successfully, you can get the URL by running the below code:
print(driver.current_url)
Using python selenium, I did: driver.get(base_url) and it
The only problem is: it went into the tab in the background(attached picture no.2), how do click on that tab which will it to the front?
I think that's an windows handles issue.
you can have your web driver focus on second tab with the below code :
driver.switch_to.window(driver.window_handles[1])
Hi I want to be able to grab the url of the currently open tab in a web browser. I can do this with Selenium but it requires that the web browser is opened with Selenium's webdriver. I instead want to be able to run this script with a Firefox or Chrome window already open, and have it return the url of the currently open tab.
I am trying to scrape data from a website, there is an url which lands me a particular page, there we have links of some items, if I click on those links, it opens in a new tab, and I can extract data from there,
But after extracting the data, I want to close the tab return to the main page and click on another link.
I am using selenium with chrome web driver. I have tried following code:
#links which lands me to a new tab
browser.find_element_by_xpath('//*[#id="data"]/div[1]/div[2]/a').click()
browser.switch_to.window(browser.window_handles[0])
browser.close() #it closes the main page, not the new tab I want
and following code,
browser.find_element_by_css_selector('body').send_keys(Keys.CONTROL + 'w') #this code didn't work.
How to close newly constructed tab using selenium and python ?
You could try this solution.
# New tabs will be the last object in window_handles
driver.switch_to.window(driver.window_handles[-1])
# close the tab
driver.close()
# switch to the main window
driver.switch_to.window(driver.window_handles[0])
Reference: http://antlong.com/common-operations-working-with-tabs-in-webdriver/
I am using the Selenium Python Bindings for web browser automation in Chrome. As part of the automation script, I click on a link and the website opens the page in a new tab. However, the WebDriver object in my python script is still pointing to the first tab.
I have tried all of the options offered on this answer but none were successful.
The only code I have been able to get to work so far is this:
driver.switch_to.window(driver.window_handles[1])
The problem I have with this is that I'm afraid I can't guarantee that the new tab will be at index 1, nor do I think I can guarantee that the new tab is at the last index. I did try using keys like this:
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
But using some logging I saw that the driver was still pointing to the first tab. Is there a way to focus on the current tab that chrome is showing?
Whenever I have cases like these I switch to the last window handle, so far it worked for me:
driver.switch_to.window(driver.window_handles[-1])