Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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 - Fatal编程技术网

如何使用python和selenium在新选项卡中打开链接

如何使用python和selenium在新选项卡中打开链接,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我想在新选项卡中打开我在网站上找到的链接。我试着打开一个新标签,并按照建议将链接的url传递给驱动程序,但是,新标签根本无法打开。(关于如何打开新选项卡,还有一些其他建议,但似乎没有一个适合我。) 因此,我最近的尝试是右键单击链接并按“t”以在新选项卡中打开链接,如下所示: 从selenium导入webdriver 从selenium.webdriver.common.keys导入密钥 从selenium.webdriver.common.action\u链导入ActionChains #使用F

我想在新选项卡中打开我在网站上找到的链接。我试着打开一个新标签,并按照建议将链接的url传递给驱动程序,但是,新标签根本无法打开。(关于如何打开新选项卡,还有一些其他建议,但似乎没有一个适合我。)

因此,我最近的尝试是右键单击链接并按“t”以在新选项卡中打开链接,如下所示:

从selenium导入webdriver
从selenium.webdriver.common.keys导入密钥
从selenium.webdriver.common.action\u链导入ActionChains
#使用Firefox访问web
driver=webdriver.Firefox()
#打开网站
司机,上车https://registers.esma.europa.eu/publication/searchRegister?core=esma_registers_firds')
#搜索信息
elem=驱动程序。通过\u id('keywordField')查找\u元素
元素清除()
元素发送密钥('XS1114155283')
button=驱动程序。通过\u id(“searchSolrButton”)查找\u元素
按钮。单击()
table_body=driver.find_element_by_xpath(“//table[@id='T01']/tbody”)
用于表体中的链接。按标签名称(“a”)查找元素:
act=动作链(驱动程序)
动作.上下文\单击(链接)
动作:发送按键(“t”)
表演
# ... 在新选项卡中执行某些操作,关闭选项卡,然后打开下一个链接。。。
但是,我在
act.perform()
上收到一条错误消息,内容如下

MoveTargetOutOfBoundsException: (974, 695) is out of bounds of viewport width (1366) and height (654)

我通过在新窗口中打开链接来解决问题,但我更喜欢选项卡版本,因为打开新浏览器窗口比打开新选项卡需要更长的时间。

您可以使用
驱动程序。执行
函数在新选项卡中打开链接

从selenium导入webdriver
#使用Firefox访问web
driver=webdriver.Firefox()
#打开网站
司机,上车https://registers.esma.europa.eu/publication/searchRegister?core=esma_registers_firds')
#搜索信息
elem=驱动程序。通过\u id('keywordField')查找\u元素
元素清除()
元素发送密钥('XS1114155283')
button=驱动程序。通过\u id(“searchSolrButton”)查找\u元素
按钮。单击()
table_body=driver.find_element_by_xpath(“//table[@id='T01']/tbody”)
用于表体中的链接。按标签名称(“a”)查找元素:
href=link.get_属性('href')
#在新选项卡中打开
driver.execute_脚本(“window.open('%s','_blank')”%href)
#切换到新选项卡
driver.switch_to.window(driver.window_句柄[-1])
#继续你的代码

能否请您提供正确的
仪器识别码
,我们在搜索结果中没有收到任何关于
XS1114155283
@supputuri的详细信息这是一个有效的ISIN。几个小时前,该网站似乎无法正常工作。这工作正常,谢谢。但它会打开一个新窗口,而不是一个新选项卡。这并不重要,但出于好奇,你知道如何打开一个新的标签吗?还有,你知道为什么右键点击链接不起作用吗?我在Chrome上测试了这段代码,因为Firefox给了我一个例外。尝试添加参数
\u blank
驱动程序。执行\u脚本(“window.open('%s','u blank')“%href)
并检查是否有效。关于右键单击,我无法解释为什么不起作用,我说的唯一建议是尽可能避免使用selenium模拟鼠标事件,这总是一件令人头痛的事。
\u blank
arguemnt没有效果。不过,我之前的评论是。看来关于这一点的讨论有点徒劳无益。