Selenium doesn't follow click to open new tab


Anonymous 0441

For a long time I had a problem using selenium to find the button I was looking for on the page. After a week, I had a "nice" idea to check the url where selenium was searching for the button. Fake me, this is the wrong url.

So the problem is, selenium searches for a specific item in page1. Then click it and according to the site design it will open page2 in a new tab. How can I get selenium to follow clicks and work on new tabs?

I though using beautiful soup to copy the url for page 1, the site doesn't show the url. Instead, it shows the function to get the url. This is really weird and confusing. idea?

all_matches = driver.find_elements_by_xpath("//*[text()[contains(., 'Pink')]]")
item = all_matches[0]
actions.move_to_element(item).perform()
item.click()


try:
    print (driver.current_url)
    get_button = driver.find_elements_by_xpath('//*[@id="getItem"]')
except:
    print 'Cant find button'
else:
    actions.move_to_element(get_button).perform()
    get_button.click()
Blakesley

Selenium treats tabs like windows, so in general switching to a new window/tab is as simple as this:

driver.switch_to.window(driver.window_handles[-1])

You may find it helpful to use the var trace window:

main_window = driver.current_window_handle
page2_window = driver.window_handles[-1]
driver.switch_to.window(page2_window)

Note that when you want to close a new window/tab, you have to close and switch back at the same time:

driver.close()
driver.switch_to.window(main_window)

Related


Left click on link doesn't work, but "open in new tab" works

rock degree I'm working on my personal website (built from a template) and when I'm done, my links stop responding to left clicks. They work fine if you right click and select "Open in New Tab". You can see the URL in the status bar at the bottom of the browse

Left click on link doesn't work, but "open in new tab" works

rock degree I'm working on my personal website (built from a template) and when I'm done my links stop responding to left clicks too. They work fine if you right click and select "Open in New Tab". You can see the URL in the status bar at the bottom of the bro

Left click on link doesn't work, but "open in new tab" works

rock degree I'm working on my personal website (built from a template) and when I'm done my links stop responding to left clicks too. They work fine if you right click and select "Open in New Tab". You can see the URL in the status bar at the bottom of the bro

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

How to command click (open in new tab) in selenium

Joey I have an array of selenium.webdriver.remote.webelement.WebElements . In order to loop through the elements and keep the session, I need to open each element in a new tab, extract the data, and then close the tab. I see that there is a way to open a new t

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and need to validate the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and conti

Selenium C# Open "New" tab CTRL+T doesn't work with CHROME

crush static void Main() { IWebDriver driver = new ChromeDriver(); driver.Navigate().GoToUrl("http://google.com"); IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + "t"); } This is the code I'm trying to

Selenium C# Open "New" tab CTRL+T doesn't work with CHROME

crush static void Main() { IWebDriver driver = new ChromeDriver(); driver.Navigate().GoToUrl("http://google.com"); IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + "t"); } This is the code I'm trying to

Selenium C# Open "New" tab CTRL+T doesn't work with CHROME

crush static void Main() { IWebDriver driver = new ChromeDriver(); driver.Navigate().GoToUrl("http://google.com"); IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + "t"); } This is the code I'm trying to

Selenium C# Open "New" tab CTRL+T doesn't work with CHROME

crush static void Main() { IWebDriver driver = new ChromeDriver(); driver.Navigate().GoToUrl("http://google.com"); IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + "t"); } This is the code I'm trying to

Selenium C# Open "New" tab CTRL+T doesn't work with CHROME

crush static void Main() { IWebDriver driver = new ChromeDriver(); driver.Navigate().GoToUrl("http://google.com"); IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + "t"); } This is the code I'm trying to

Selenium C# Open "New" tab CTRL+T doesn't work with CHROME

crush static void Main() { IWebDriver driver = new ChromeDriver(); driver.Navigate().GoToUrl("http://google.com"); IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + "t"); } This is the code I'm trying to

The link doesn't open in a new tab

Alan Weber Here is my javascript code, I want the link should open in new tab but it doesn't happen, how can I do that? <link href="http://vjs.zencdn.net/5.4.6/video-js.min.css" rel="stylesheet"> <script src="http://vjs.zencdn.net/5.4.6/video.min.js"></script>

Middle click doesn't always open links in Chrome's new tab

Amireguez I use the middle mouse button to open links in new tabs and close tabs. But sometimes, middle-clicking opens the link in the current tab instead of a new tab. It's not random - I've noticed that it usually happens when a popup link is clicked, such a

Middle click doesn't always open links in Chrome's new tab

Amireguez I use the middle mouse button to open links in new tabs and close tabs. But sometimes, middle-clicking opens the link in the current tab instead of a new tab. It's not random - I've noticed that it usually happens when a popup link is clicked, such a

Ctrl + t - Can't open new tab in Selenium + Firefox Python

Wren Has Selenium removed any functionality? Goal: Open a new tab like "Ctrl+t" environment: Windows 7 Firefox 68 Python 3.7 The following two scripts are unresponsive: # 1 dr.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't') # 2 ActionChains(dr

Ctrl + t - Can't open new tab in Selenium + Firefox Python

Wren Has Selenium removed any functionality? Goal: Open a new tab like "Ctrl+t" environment: Windows 7 Firefox 68 Python 3.7 The following two scripts are unresponsive: # 1 dr.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't') # 2 ActionChains(dr

Java Selenium: open new tab

Danny Cobb So I URLwant to browse this link in a new tab, which is not clickable, so when I click this button, nothing happens and will not work (even if not manually): WebElement hrefLink; actions.keyDown(Keys.SHIFT).click(hrefLink).keyUp(Keys.SHI