Reputation: 85
There is button which on clicking navigates to facebook login page and I want to confirm it navigates properly using selenium
. However on button click the facebook login opens in a new tab but the driver.title
returns the title of previous tab (where the button is present).
def test_01_facebook(self):
self.driver.find_element_by_xpath("//i[@class='fa fa-facebook-square']").click()
title = self.driver.title
self.assertTrue("Facebook" == self.driver.title)
print (title)
Alternatively I could compare the url using driver.current_url
but the issue is the new tab url has a lengthy string after https://www.facebook.com/login.php?
.
Upvotes: 0
Views: 2013
Reputation: 22
Since the facebook is opened in a new tab, so you need switch to the new tab first, please try below code:
self.driver.find_element_by_xpath("//i[@class='fa fa-facebook-square']").click()
windowHandle = self.driver.window_handles
for windowId in self.driver.window_handles:
if not windowId==windowHandle
self.driver.switch_to.window(windowId);
title = self.driver.title
self.assertTrue("Facebook" == self.driver.title)
print (title)
Upvotes: 0
Reputation: 193068
To extract the Page Title from the newly opened TAB i.e. Facebook – log in or sign up you need to:
You can use the following solution:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_01_facebook(self):
windows_before = driver.current_window_handle
self.driver.find_element_by_xpath("//i[@class='fa fa-facebook-square']").click()
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
windows_after = driver.window_handles
new_window = [x for x in windows_after if x != windows_before][0]
driver.switch_to_window(new_window)
WebDriverWait(driver, 10).until(EC.title_contains("log in"))
self.assert "Python" in driver.title
print (title)
Upvotes: 0
Reputation: 52665
If LogIn page opens in new tab, you should wait for new tab and switch to it to check title:
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
def test_01_facebook(self):
self.driver.find_element_by_xpath("//i[@class='fa fa-facebook-square']").click()
current = self.driver.current_window_handle
wait(self.driver, 10).until(EC.new_window_is_opened(self.driver.window_handles))
self.driver.switch_to.window([w for w in self.driver.window_handles if w != current][0])
title = self.driver.title
self.assertTrue("Facebook" == self.driver.title)
print (title)
You might also switch back to main window using
self.driver.switch_to.window(current)
Upvotes: 1