有一个按钮,单击该按钮可以导航到Facebook登录页面,我想确认它使用selenium
可以正确导航。但是,单击按钮后,facebook登录会在新选项卡中打开,但是driver.title
返回上一个选项卡的标题(显示该按钮的位置)。
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)
或者,我可以使用driver.current_url
比较网址,但是问题是新标签页的网址在https://www.facebook.com/login.php?
之后有一个长字符串。
答案 0 :(得分:1)
如果“登录”页面在新选项卡中打开,则应等待新选项卡并切换到该页面以检查标题:
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)
您也可以使用
切换回主窗口self.driver.switch_to.window(current)
答案 1 :(得分:0)
要从新打开的 TAB 中提取页面标题,即 Facebook –登录或注册,您需要:
您可以使用以下解决方案:
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)
答案 2 :(得分:0)
由于在新标签页中打开了Facebook,因此您需要先切换到新标签页,请尝试以下代码:
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)