user3289968
user3289968

Reputation: 97

Open link using Selenium on new page

I am clicking the link "Images" on a new page (after searching 'bugs bunny') on Google. It is not retrieving images of the search, rather it is opening the link 'Images' on the old page.

My Code:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get('http://www.google.com')

search = browser.find_element_by_name('q')
search.send_keys("bugs bunny")
search.send_keys(Keys.RETURN) # hit return after you enter search text
browser.current_window_handle
print(browser.current_url)
browser.find_element_by_link_text("Images").click()

Upvotes: 0

Views: 334

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146510

Your problem is you are using send_keys, which perform the action and don't wait

search.send_keys(Keys.RETURN) # hit return after you enter search text

So after that if you use click it is doing it nearly on the current page even when the results are not loaded. So you need to add some delay for the return key to change the results and once the results are loaded, you can do the click

So what you need is a simple sleep delay

Upvotes: 1

Related Questions