0

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?.

5
  • 1
    Please explain how this question relates to the tag "coded-ui-tests" or if it does not then please remove the tag. Commented Nov 19, 2018 at 9:13
  • I believe coded-ui-tests tag is used for automated tests that drive applications through its user interface for functional testing of the UI controls. The above code is an automated script to to test the functionality of button, so I think it relates. Please do correct me if i'm wrong. @AdrianHHH Commented Nov 19, 2018 at 9:22
  • 2
    Coded UI Tests are a specific type of test supported by and implemented with Visual Studio. It is not a generic style of UI testing. Hover the mouse over the tag in the question or view this page: stackoverflow.com/tags/coded-ui-tests/info Commented Nov 19, 2018 at 9:26
  • I'm new to this UI testing and I mistook coded-ui-tests for automated ui testing turns out it is a different tool like selenium used for UI testing. Thanks @AdrianHHH for the correction. Commented Nov 19, 2018 at 9:57
  • @Atul Consider evaluating all the answer's you receive against your question and provide a feedback to the answer contributors. It takes some serious efforts to construct canonical answers. Commented Nov 19, 2018 at 10:23

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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)

1 Comment

Yep that was the issue. self.driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + Keys.TAB) was not working for me. But I found this one to be working self.driver.switch_to_window(self.driver.window_handles[-1])
0

To extract the Page Title from the newly opened TAB i.e. Facebook – log in or sign up you need to:

  • Switch the focus to the newly opened TAB inducing WebDriverWait
  • You need to induce WebDriverWait again for the Page Title to render.
  • 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)
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.