如何在Selenium Python中解决此问题?

时间:2020-05-12 18:55:25

标签: python selenium

在此Web应用程序的“主页”上,导航栏上有一个“支持”图标,用户可以单击该图标以也打开“支持”页面。好像浏览器中有2个标签,一个是主页,一个是支持页面。

在我的检查情况下,我想验证如果用户单击此图标,它确实到达“支持”页面。这是我的一般代码:

driver = webdriver.Firefox()
url = "https://mainpage.xyz.com"
driver.get(url)

by = By.CSS_SELECTOR
support = "..."
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((by, support))).click()
assert "support" in driver.current_url

但是,以某种方式在断言期间,我所看到的是driver.current_url的值仍然是“ mainpage.xyz.com”的值。有解决这个问题的主意吗?

谢谢

杰克

1 个答案:

答案 0 :(得分:0)

我刚刚用Google搜索,并找到了这样的解决方案:

driver = webdriver.Firefox()
url = "https://mainpage.xyz.com"
driver.get(url)

by = By.CSS_SELECTOR
    support = "..."
WebDriverWait(driver, 20).until(EC.presence_of_element_located((by,support))).click()

# this call switch from the main tab to the support tab. 
# and I can have the url asserted after that.      
driver.switch_to.window(driver.window_handles[-1])
assert "support" in driver.current_url

# this call switch back from support tab to mainpage tab.  
driver.switch_to.window(driver.window_handles[0])