Ranjan Pandey
Ranjan Pandey

Reputation: 85

How to getText of an element from a search result using Selenium Webdriver

I am trying to pick a URL from the search results on Tripadvisor.com using its element but all my efforts have been unsuccessful. Here is my code:

from selenium.webdriver.common.keys import Keys
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
import time
driver = webdriver.Chrome()
base_url = 'https://www.tripadvisor.in/'
driver.get(base_url)

hotel = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'mainSearch')))
#hotel = driver.find_element_by_id("mainSearch")
hotel.send_keys('the leela palace')
time.sleep(0.5)

loc = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'GEO_SCOPED_SEARCH_INPUT')))
#loc = driver.find_element_by_id("GEO_SCOPED_SEARCH_INPUT")
loc.send_keys('New Delhi, India')
time.sleep(0.5)

search = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'SEARCH_BUTTON')))
search.submit()
time.sleep(1)

url = driver.find_element_by_class_name("dp_cr.loc1759051")
page_url = url.getAttribute("data-url")

Here is the html portion of the website that am trying to get the information from:

<div class="dp_cr loc1759051" data-url="/Hotel_Review-g304551-d1759051-Reviews-The_Leela_Palace_New_Delhi-New_Delhi_National_Capital_Territory_of_Delhi.html?t=304551">
<div class="date_picker_wrap">
<div class="prw_rup prw_datepickers_desktop_horizontal_styleguide_icon" data-prwidget-name="datepickers_desktop_horizontal_styleguide_icon" data-prwidget-init="handlers">
<span class="unified-picker" data-datetype="CHECKIN" data-emptytext="Check In" data-trackingcontext="CheckIn|DATES" data-didcreatedefaultdates="false" data-dateformat="dd/MM/yyyy" onclick="(ta.prwidgets.getjs(this,'handlers')).onDatePickerClick(this, false);">
<span class="picker-inner">
<span class="ui_icon calendar"></span> 
<span class="picker-label"> Check In </span></span></span>
<span class="unified-picker" data-datetype="CHECKOUT" data-emptytext="Check Out" data-trackingcontext="CheckOut|DATES" data-dateformat="dd/MM/yyyy" onclick="(ta.prwidgets.getjs(this,'handlers')).onDatePickerClick(this, false);"><span class="picker-inner"><span class="ui_icon calendar"></span> <span class="picker-label"> Check Out </span></span></span></div></div><div class="crOverlayButton"><span class="ui_button original large w100p" onclick="ta.page.gatherDatesNear(this);">Show Prices </span></div></div>

I am trying to get the data-url portion of the script so that I can concat it to my base url and jump to the page. Also, if there is a way that I can directly click on the search result link, it would be more easier. But am not able to find the element for the search result.

Upvotes: 0

Views: 165

Answers (2)

JeffC
JeffC

Reputation: 25611

The easiest way to navigate to the search result pages is to just click on each result. You can do this using the CSS selector below

driver.find_elements_by_css_selector("div.result_wrap")

This will return a collection of the search results and then you can click any one you want and it will take you to the result page.

Upvotes: 1

Akarsh
Akarsh

Reputation: 967

Corrections to your code.

  • Instead of className use css selector(we can not use classname if it is having spaces in its name)

  • For webelement to get attribute value,we will use webelement.get_attribute method but not
    getAttribute.

Replace your last two lines of code with below code.

url = driver.find_element_by_css_selector(".dp_cr.loc1759051")
page_url = url.get_attribute("data-url")

Let me know if it works for you.

Upvotes: 2

Related Questions