使用 Selenium 在选项卡之间切换并对个人执行操作 [英] switch between tabs and perform action on individual using Selenium
本文介绍了使用 Selenium 在选项卡之间切换并对个人执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试提取 URL,将它们打开到一个新选项卡中,然后执行一些操作.我的代码是-
urls = self.driver.find_elements_by_xpath('//div[@id="maincontent"]/table/tbody/tr/td//a[@href]')对于网址中的网址:尝试:href = url.get_attribute("href")[28:]打印href动作 = 动作链(self.driver)url_1 = self.driver.find_element_by_xpath('//*[@href="'+href+'"]')actions.key_down(Keys.CONTROL).click(url_1).key_up(Keys.CONTROL).perform()除了例外,e:打印e动作1 =动作链(self.driver)actions1.key_down(Keys.CONTROL).key_down(Keys.TAB).key_up(Keys.TAB).key_up(Keys.CONTROL).perform()打印嗨"show_report = self.driver.find_element_by_xpath('//*[@value="Show Report(s)"]')show_report.click()
它在新选项卡中打开 URL,但不对它们执行操作.我该怎么办??
解决方案
您需要将焦点转移到新选项卡上,因为它们通常在后台打开.然后你需要抓住当前标签的句柄.
在此处对此进行了描述:
<块引用>import selenium.webdriver 作为 webdriver导入 selenium.webdriver.support.ui 作为 ui从 selenium.webdriver.common.keys 导入密钥从时间导入睡眠浏览器 = webdriver.Firefox()browser.get('https://www.google.com?q=python#q=python')first_result = ui.WebDriverWait(browser, 15).until(lambda 浏览器:browser.find_element_by_class_name('rc'))first_link = first_result.find_element_by_tag_name('a')# 保存开窗器(当前窗口,不要误认为是tab...不一样)main_window = browser.current_window_handle# 通过在元素上发送击键来在新选项卡中打开链接# 使用:Keys.CONTROL + Keys.SHIFT + Keys.RETURN 打开堆栈顶部的选项卡first_link.send_keys(Keys.CONTROL + Keys.RETURN)# 切换标签到新标签,我们假设它是右边的下一个browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)# 将焦点放在当前窗口上,实际上将焦点放在当前可见的选项卡上browser.switch_to_window(main_window)#在这个页面上做任何你必须做的事情,我们现在就睡吧睡觉(2)# 关闭当前标签browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')# 将焦点放在当前窗口上,这将是窗口开启器browser.switch_to_window(main_window)
I am trying to extract URLs, open them into a new tab and then perform some actions. My code is-
urls = self.driver.find_elements_by_xpath('//div[@id="maincontent"]/table/tbody/tr/td//a[@href]')
for url in urls:
try:
href = url.get_attribute("href")[28:]
print href
actions = ActionChains(self.driver)
url_1 = self.driver.find_element_by_xpath('//*[@href="'+href+'"]')
actions.key_down(Keys.CONTROL).click(url_1).key_up(Keys.CONTROL).perform()
except Exception, e:
print e
actions1 = ActionChains(self.driver)
actions1.key_down(Keys.CONTROL).key_down(Keys.TAB).key_up(Keys.TAB).key_up(Keys.CONTROL).perform()
print "hi"
show_report = self.driver.find_element_by_xpath('//*[@value="Show Report(s)"]')
show_report.click()
It opens the URLs in new tabs but is not performing action on them. What should I do??
解决方案
You'll need to change focus to the new tab as they usually get opened in the background. Then you'll need to grab a handle for the now current tab.
This is described here:
import selenium.webdriver as webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.common.keys import Keys from time import sleep browser = webdriver.Firefox() browser.get('https://www.google.com?q=python#q=python') first_result = ui.WebDriverWait(browser, 15).until(lambda browser: browser.find_element_by_class_name('rc')) first_link = first_result.find_element_by_tag_name('a') # Save the window opener (current window, do not mistaken with tab... not the same) main_window = browser.current_window_handle # Open the link in a new tab by sending key strokes on the element # Use: Keys.CONTROL + Keys.SHIFT + Keys.RETURN to open tab on top of the stack first_link.send_keys(Keys.CONTROL + Keys.RETURN) # Switch tab to the new tab, which we will assume is the next one on the right browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB) # Put focus on current window which will, in fact, put focus on the current visible tab browser.switch_to_window(main_window) # do whatever you have to do on this page, we will just got to sleep for now sleep(2) # Close current tab browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w') # Put focus on current window which will be the window opener browser.switch_to_window(main_window)
这篇关于使用 Selenium 在选项卡之间切换并对个人执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文