python - How to getText of an element from a search result using Selenium Webdriver -
i trying pick url search results on tripadvisor.com using element efforts have been unsuccessful. here code:
from selenium.webdriver.common.keys import keys selenium import webdriver selenium.webdriver.support.ui import webdriverwait selenium.webdriver.common.by import selenium.webdriver.support import expected_conditions 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 html portion of website trying 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 trying data-url portion of script can concat base url , jump page. also, if there way can directly click on search result link, more easier. not able find element search result.
corrections code.
instead of classname use css selector(we can not use classname if having spaces in name)
for webelement attribute value,we use webelement.get_attribute method not
getattribute.
replace last 2 lines of code below code.
url = driver.find_element_by_css_selector(".dp_cr.loc1759051") page_url = url.get_attribute("data-url")
let me know if works you.
Comments
Post a Comment