Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用机器人框架selenium打开新窗口?_Python_Selenium_Selenium Webdriver_Webdriver_Robotframework - Fatal编程技术网

Python 如何使用机器人框架selenium打开新窗口?

Python 如何使用机器人框架selenium打开新窗口?,python,selenium,selenium-webdriver,webdriver,robotframework,Python,Selenium,Selenium Webdriver,Webdriver,Robotframework,在我的测试页面上有一个链接,默认情况下在“新建”选项卡中打开。我需要打开链接,并在新打开的页面上验证一些值。 因为我发现selenium不支持使用选项卡,所以我尝试在新窗口中打开链接,但它仍然不起作用。。 我实现了python函数来按住SHIFT键(我以前对CTRL做过这样的操作,它可以正常工作),然后调用了“click”函数,但链接仍然在新选项卡中打开 from robot.libraries.BuiltIn import BuiltIn from selenium.webdriver.com

在我的测试页面上有一个链接,默认情况下在“新建”选项卡中打开。我需要打开链接,并在新打开的页面上验证一些值。 因为我发现selenium不支持使用选项卡,所以我尝试在新窗口中打开链接,但它仍然不起作用。。 我实现了python函数来按住SHIFT键(我以前对CTRL做过这样的操作,它可以正常工作),然后调用了“click”函数,但链接仍然在新选项卡中打开

from robot.libraries.BuiltIn import BuiltIn
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

class CustomSeleniumLibrary(object):
    def __init__(self):
        self.driver = None
        self.library = None
        self.ac = None

    def get_library_instance(self):
        if self.library is None:
            self.library = BuiltIn().get_library_instance('ExtendedSelenium2Library')
        return self.library

    def get_action_chain(self):
        if self.ac is None:
            self.ac = ActionChains(self.get_library_instance()._current_browser())
        return self.ac

def hold_shift(self):
        actionChain = self.get_action_chain()
        actionChain.key_down(Keys.SHIFT)
        actionChain.perform()
机器人关键字是

Open project detail
     wait until element is visible  ${LINK_TO_PROJECT}
     ${project}=  get text  ${LINK_TO_PROJECT}
     hold shift
     click element   ${LINK_TO_PROJECT}
     #sleep  2s
     #release shift
     element should contain   //h3  Project Details: ${project}

我尝试了许多睡眠的变体,释放了钥匙等,但它从来没有真正打开新窗口中的链接。我还尝试验证新打开的选项卡中的数据(不尝试在新窗口中打开),但它总是很快重定向到原始选项卡,因此新选项卡上的DOM尚未加载。。谢谢你的建议

您可以使用以下代码来处理在新选项卡中打开的页面:

current = driver.current_window_handle
driver.find_element_by_css_selector('a').click() # use own selector
new_tab = [tab for tab in driver.window_handles if tab != current][0]
driver.switch_to.window(new_tab)
# do some actions
driver.close()
driver.switch_to.window(current)
此外,您还可以进行一些修改(不推荐,但…),以避免处理新选项卡,并强制在当前选项卡中打开链接:

link = driver.find_element_by_css_selector('a')
driver.execute_script('arguments[0].target="_self";', link)

为什么您认为
selenium
不支持使用选项卡?我在几个线程中找到了它…我还找到了一些解决方案,其中用户在代码中手动传递链接,但我不知道链接中的地址是什么..但它可能被视为href的值,然后作为变量传递..will CheckThank,解决方案很好:)刚才我还需要打开新的空选项卡(不点击任何链接,只打开它进行复制粘贴操作),所以我调用了包含这个self.get_action_chain().key_down(Keys.CONTROL).key_down('t').perform()的函数,但什么也没有发生…:(我尝试了许多变体,使用send_键,组合键down和send_键,但仍然没有打开任何选项卡…有什么想法吗?使用
selenium
可以打开新选项卡,如
driver.execute_脚本(“window.open”()http://google.com')")
。如果不将
URL
作为参数传递,则只会打开空选项卡hi,请问,您知道如何关闭新选项卡吗?当切换到新选项卡时,我尝试了关闭(),但它关闭了整个窗口…我找不到任何工作解决方案:(Hm…
driver.close()
应该这样做。如果它不起作用,你也可以尝试
driver.exexcute_script('window.close();')
在这两种情况下,它都会关闭整个窗口,而不仅仅是选项卡:/I我现在在测试中还有一些其他的解决方法,但它很奇怪……无论如何,非常感谢!:)