简体   繁体   English

如何在Python的Selenium Webdriver的新选项卡中禁用打开页面?

[英]How to disable opening the page in a new tab in Selenium Webdriver in Python?

I am scraping the site : http://www.delhidistrictcourts.nic.in/DLCIS-2016-2.html 我正在抓网站: http//www.delhidistrictcourts.nic.in/DLCIS-2016-2.html

There are a number of links in this page. 此页面中有许多链接。 The user would click on any of these links ( via selenium web driver). 用户可以点击任何这些链接(通过selenium web驱动程序)。 The problem is, when the user clicks on these links, it opens in a new tab because all the links has an attribute ( " _target=blank ") 问题是,当用户点击这些链接时,它会在新标签页中打开,因为所有链接都有一个属性(“ _target=blank ”)

Any idea how to force, the links to open in the same tab ? 知道如何强制,链接在同一个标​​签中打开?

Here is the code I have written 这是我写的代码

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
url = 'http://www.delhicourts.nic.in/DLCIS-2016-2.html'

driver=webdriver.Chrome()
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get(url)

try:
    wait.until(EC.presence_of_element_located((By.CLASS_NAME, "submit1"))).click()
except Exception as e:
    print str(e)

在此输入图像描述

You can try to update the value of @target : 您可以尝试更新@target的值:

link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "CASE NUMBER WISE")))
driver.execute_script("arguments[0].target='_self';", link)
link.click()

To apply the same for all the links on page: 要对页面上的所有链接应用相同的内容:

links = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, "a")))
for link in links:
    driver.execute_script("arguments[0].target='_self';", link)

or extract @href of link and get() it: 或者提取链接的@hrefget()它:

link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "CASE NUMBER WISE")))
url = link.get_attribute('href')
driver.get(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 Selenium Webdriver 无法在 Firefox 68.0 及更高版本上打开新标签? - How to fix Selenium Webdriver not opening a new tab on Firefox 68.0 and above? 在 Python 和 Selenium WebDriver 中打开受密码保护的页面 - Opening a password protected page in Python and Selenium WebDriver 由python webdriver在chrome浏览器中的selenium新选项卡 - selenium new tab in chrome browser by python webdriver Python + Selenium WebDriver:在新标签页中打开URL - Python + Selenium WebDriver: open URL in new tab 如何在不打开新标签(python)的情况下更改Selenium中的URL? - How to change URL in Selenium without opening new tab (python)? 使用Selenium python(3)打开新选项卡并对其执行功能 - Opening new tab with selenium python(3) and performing functions on it Chrome WebDriver 直接启动到 URL 而不是新标签页 - Python、Selenium - Chrome WebDriver launch directly to URL instead of new tab page - Python, Selenium 使用selenium和python在现有标签页/窗口中打开新页面中的链接 - Open a link in a new page opening in the existing tab/window using selenium and python 使用 Selenium WebDriver 的新标签 - New Tab using Selenium WebDriver Python - Selenium:提交表单而不打开新选项卡或新窗口 - Python - Selenium : Submit form without opening new tab or new window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM