7

I am scraping the site : 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). 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")

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)

enter image description here

1 Answer 1

7

You can try to update the value of @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:

link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "CASE NUMBER WISE")))
url = link.get_attribute('href')
driver.get(url)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot @Anderson for your help. link = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "submit1"))) is a form submission(submit button) and not a link.
Yep, but there was no code for link in your question, so... just apply the same for link node
that is the part of the code I am missing. The user will be selecting from a number of links which takes him to the new tab. This tab has a form for which I have written the code above
how to force, the links to open in the same tab? presumes that link is already found... Do you need to locate the link or to open it in the same page? Update your question accordingly
That is correct. I will not know the link because there are a number of links in the URL. Each of these link will take you to a page ( in a new tab). I would need to open the link which the user clicks in the same page. I will modify the question with additional screenshots

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.