在新标签页中打开“ href”变量

本杰明

我在python中使用selenium和chrome webdriver。

我试图将“ href”存储在变量中(在本例中为“ link”),然后在新标签页中将其打开。

我知道如何使用这种方式在新标签页中打开专用网站:

driver.execute_script("window.open('http://www.example.com', 'newtab')")

但是使用windows.open脚本仅接受直接文本(据我所知),而不接受变量。

这是代码:

link = driver.find_element_by_class_name('asset-content').find_element_by_xpath(".//a[@class='mr-2']").get_attribute("href") #assigning 'href' into link variable. works great. 
driver.execute_script("window.open(link, 'newtab')") #trying to open 'link' in a new tab

错误:

unknown error: link is not defined

还有其他方法可以在新标签页中打开“链接”变量吗?

利特文

您将字符串传递给execute_script,因此不是按字面意义传递“链接”,而是传递链接的值(连接):

driver.execute_script("window.open('"+link+"','icoTab');")

打开标签页的另一种方法是将CTRL + T发送到浏览器:

driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
driver.get(link)

如前所述,您可以在此处找到更多的信息28431765 / open-web-in-new-tab-selenium-python

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章