使用 window_handles 时 Selenium Webdriver 失败
Selenium Webdriver failed when use window_handles
我正在尝试使用 Chrome 作为浏览器处理 Python Selenium webdriver 中的两个选项卡。
如果我将 Chrome 浏览器保持为 selected window.[即前屏处理]
当我使用
将控件更改为新选项卡时
driver.switch_to_window(driver.window_handles[1])
并最小化 google chrome[即如果我 select 除了 Google Chrome] 之外的任何进程。我在查找link 文本说 找不到元素异常 仅适用于第二个选项卡而不是第一个选项卡。
我在第一个选项卡上得到结果。
def DriverCreation():
try:
Driver = WebBase.initWebScraping(URL) # Methods visible Driver.driver and Driver.loggerDriverWait = Driver.EC
print "Driver Creation Successful"
return Driver
except:
print "Driver Initalisation Failed"
sys.exit(1)
if __name__ == '__main__':
URL = 'https://www.example.com/'
Driver = DriverCreation() # will Load first Tab with www.Example.com
aboutlink = Driver.driver.find_element_by_link_text('about')
aboutlink.send_keys(Keys.CONTROL + Keys.RETURN)
Driver.driver.switch_to_window(Driver.driver.window_handles[1])
contactLink = Driver.driver.find_element_by_link_text('contact')
print contactLink.text() #** getting error if i change the focus from Google Chrome and works fine if i keep the window focus on Google Chrome**
您可以使用以下代码管理选项卡。
driver.execute_script("window.open('"+url+"', '_blank');")
driver.switch_to_window(driver.window_handles[1])
我正在尝试使用 Chrome 作为浏览器处理 Python Selenium webdriver 中的两个选项卡。
如果我将 Chrome 浏览器保持为 selected window.[即前屏处理]
当我使用
将控件更改为新选项卡时driver.switch_to_window(driver.window_handles[1])
并最小化 google chrome[即如果我 select 除了 Google Chrome] 之外的任何进程。我在查找link 文本说 找不到元素异常 仅适用于第二个选项卡而不是第一个选项卡。
我在第一个选项卡上得到结果。
def DriverCreation():
try:
Driver = WebBase.initWebScraping(URL) # Methods visible Driver.driver and Driver.loggerDriverWait = Driver.EC
print "Driver Creation Successful"
return Driver
except:
print "Driver Initalisation Failed"
sys.exit(1)
if __name__ == '__main__':
URL = 'https://www.example.com/'
Driver = DriverCreation() # will Load first Tab with www.Example.com
aboutlink = Driver.driver.find_element_by_link_text('about')
aboutlink.send_keys(Keys.CONTROL + Keys.RETURN)
Driver.driver.switch_to_window(Driver.driver.window_handles[1])
contactLink = Driver.driver.find_element_by_link_text('contact')
print contactLink.text() #** getting error if i change the focus from Google Chrome and works fine if i keep the window focus on Google Chrome**
您可以使用以下代码管理选项卡。
driver.execute_script("window.open('"+url+"', '_blank');")
driver.switch_to_window(driver.window_handles[1])