Switching and focusing a newly opened tab in selenium

Hello, I use selenium to click links and usually use an online application.

I'm having trouble clicking on a specific link that opens a new tab and performs an action on this newly opened tab. I have this code:

friend_link = browser.find_element_by_tag_name('a')
friend_link.click() # this is where new tab is opened

After which the webdriver (from my eyes) will open on a new tab without the need to call

browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

So, all is well. On the webdriver tab, a new tab opens. When I try to click a link on this recently opened tab, I get a “No item” exception, that is, it cannot find the item I was looking for.

Two questions:

1) Does webdriver know that an open open tab has been opened and to perform actions on this tab? Maybe I should say that. I tried

main_window = browser.current_window_handle
browser.switch_to_window(main_window)

, .

2) , ?

+4
1

driver.window_handles, instanc, . driver.switch_to_window(instance-id) . . :

>>> driver.window_handles
[u'CDwindow-608A7C64-A633-4DEC-B88F-6A2578C47669']
>>> driver.window_handles
[u'CDwindow-608A7C64-A633-4DEC-B88F-6A2578C47669', u'CDwindow-A2A95622-3146-4BF6-9E7A-7A6632A73C86']
>>> driver.current_window_handle
u'CDwindow-608A7C64-A633-4DEC-B88F-6A2578C47669'
>>> driver.switch_to.window("CDwindow-A2A95622-3146-4BF6-9E7A-7A6632A73C86")
>>> driver.current_window_handle
u'CDwindow-A2A95622-3146-4BF6-9E7A-7A6632A73C86'
>>> driver.get("http://www.amazon.com")
+2

Source: https://habr.com/ru/post/1625514/


All Articles