2

I am trying to make such a program, first go to duckduckgo.com and search on the search bar Best hotel in india,then find my website link then click the link to my website.

from time import sleep
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox(executable_path=r'C:\Users........\geckodriver.exe')

driver.get("https://duckduckgo.com")
time.sleep(10)
search_field = driver.find_element_by_id('search_form_input_homepage')
search_field.clear()

search_field.send_keys('best hotel in india')
search_field.submit()
time.sleep(10)
driver.find_element_by_link_text("More Results").click()
time.sleep(10)
driver.find_element_by_link_text("More Results").click()
time.sleep(10)
driver.find_element_by_link_text("More Results").click()
time.sleep(10)
driver.find_element_by_link_text("More Results").click()
time.sleep(10)

driver.find_element_by_link_text('//my hotel url//').click()
time.sleep(60)
driver.quit()

but i'm getting following error

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:......
2
  • What is the link to your website which is to be searched? @Yash Commented Sep 19, 2020 at 11:13
  • 1
    time.sleep(10) is wasteful. You should do instead once at the beginning driver.implicitly_wait(10). Then when you do a find_element, the driver will implicitly wait for up to 10 seconds for the element to appear before timing out. But, of course, if the element shows up before the 10 seconds, which it usually does, you do not needlessly wait longer than you have to. Commented Sep 19, 2020 at 11:37

1 Answer 1

1

See my comment about time.sleep being wasteful. There were only 2 pages worth of results so you need to test in a loop that will not cause an exception:

from time import sleep
from selenium import webdriver
#import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox(executable_path=r'C:\Users........\geckodriver.exe')
driver.implicitly_wait(10)

driver.get("https://duckduckgo.com")
search_field = driver.find_element_by_id('search_form_input_homepage')
search_field.clear()

search_field.send_keys('best hotel in india')
search_field.submit()
for _ in range(4):
    more_results = driver.find_elements_by_link_text("More Results") # note find_elements with an 's'
    if len(more_results) == 0:
        break
    more_results[0].click()
elements = driver.find_elements_by_link_text('//my hotel url//')
if len(elements) != 0:
    elements[0].click()
else:
    print('Could not find my hotel url')
input('pausing...')
driver.quit()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.