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/
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)
I have a python selenium code to test a website. Now once the driver click on a link in the 1st tab, it opens it up in a new tab, but the focus stays on the previous tab, and I am not able to reference any elements in the new tab.
How can change the focus to the new?
Also both the tabs are open in the same browser and I am using Chrome browser
name each window if you are working with a few number of windows. using this chunk you can name and switch between tabs
window1= driver.window_handles[1]
driver.switch_to_window(window1)
You can loop through the window handles, record the counts of window handles and switch with count - 1. Check below.
n = 0
for handle in driver.window_handles:
n+=1
driver.switch_to_window(n-1)
If you encounter any difficulty, please paste your code for further elucidation.
I am writing a python script that uses BeautifulSoup to web scrape and then Selenium to navigate sites. After navigating to another site using the .click() on a link I want to use .current_url to get the site url to use for beautiful soup. The problem is that the .click() opens the link in a new tab so when I use current_url I get the url of the original site.
I tried using:
second_driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
to change tabs but it has no effect on the current_url function so it didn't work.
Simplified code:
second_driver = webdriver.Firefox()
second_driver.get(<the original url>)
time.sleep(1)
second_driver.find_element_by_class_name(<html for button that opens new tab >).click()
time.sleep(1)
url=second_driver.current_url
So I want url to be the new site after click not the original url
Thank you and sorry if its obvious I am a beginner.
You'll need to switch to the new tab. webrdiver.window_handles is a list of open tabs/windows. This snippet will switch you to the second open handle.
If you want to go back to where you started, use [0]. If you want to always go to the last tab opened, use [-1]. If you try to switch to window_handles[1] before it exists, you'll raise an IndexError.
webdriver.switch_to_window(webdriver.window_handles[1])
I have a Python code use the Selenium Chrome driver to click multiple tags in a list.
Each opens a new window where a file is downloaded instantly, and that is all good; however, some of these new windows will have two files as options to download, so no file is downloaded until the next tag is clicked.
The problem I have is that I can't let Selenium know how to "check" if it should just close the window since the download was automatic, or if it should do another click to begin the download.
I tried to do a find_elements_by_(all possible methods) to have the driver check if there are any download links, but if the download is automatic, the driver just keeps running. It doesn't turn an error or anything, but it just runs perpetually. If I close the actual download window, it THEN shows a result.
from selenium import webdriver
browser = webdriver.Chrome('chromedriver.exe', options=options)
#options are set for Chrome to download a PDF file without viewing it first.
main_window = browser.window_handles[0]
#this is the initial click to open the download window
browser.find_elements_by_css_selector('selector path').click()
#this is to have the driver activate the new window
pops = browser.window_handles[1]
browser.switch_to.window(pops)
#attempt to find the <a> tag with the download item
if browser.find_elements_by_css_selector('selector path') != []:
time.sleep(1)
browser.find_elements_by_css_selector('selector path').click()
browser.close()
browser.switch_to.window(main_window)
#this works fine when there are tags in the window. No problems
else:
time.sleep(1)
browser.close()
browser.switch_to.window(main_window)
#code can't get to the "else" because the driver gets stuck checking for the selector perpetually.
What I expect is Selenium to check if the window has content, if not, delay for a second and close the window. If there IS content, click the link to download; however, the driver just searches for the selector perpetually.
EDIT: Forgot to mention the window that starts the download automatically doesn't have any content aside html, script, and head tags.
Even when I try to run a find_element_by_tag, Selenium doesn't actually find the content.
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');")