我正在抓取网站:http://www.delhidistrictcourts.nic.in/DLCIS-2016-2.html
此页面中有许多链接。用户可以点击任何这些链接(通过selenium web驱动程序)。问题是,当用户点击这些链接时,它会在新标签页中打开,因为所有链接都有一个属性(" _target=blank
")
任何想法如何强制,链接在同一个标签中打开?
这是我写的代码
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)
答案 0 :(得分:2)
您可以尝试更新@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()
对页面上的所有链接应用相同的内容:
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)
或提取链接的@href
及其get()
:
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "CASE NUMBER WISE")))
url = link.get_attribute('href')
driver.get(url)