Jia
Jia

Reputation: 2581

How to locate a tag by text in its child tag

I am new to Webelement and selenium, can anyone help me on how to locate element below , using text "Hotel Wahington":

<a class="hotel_name_link url" href="/hotel/nl/washington.html?label=gen173nr-1FCAEoggI46AdIM1gEaFCIAQGYATG4ARjIAQzYAQHoAQH4AQKIAgGoAgS4Apyz__EFwAIB&amp;sid=1b691d9ad57ac7ee7c3d40dac2f7f488&amp;dest_id=-2140479&amp;dest_type=city&amp;group_adults=2&amp;group_children=0&amp;hapos=1&amp;hpos=1&amp;no_rooms=1&amp;sr_order=popularity&amp;srepoch=1581242789&amp;srpvid=87de4712b9a90095&amp;ucfs=1&amp;from=searchresults;highlight_room=#hotelTmpl" target="_blank" rel="noopener">
  <span class="sr-hotel__name" data-et-click="    ">Hotel Washington</span>
  <span class="invisible_spoken">Opens in new window</span>
</a>

Adding the original link below : https://www.booking.com/searchresults.html?label=gen173nr-1FCAEoggI46AdIM1gEaFCIAQGYATG4ARjIAQzYAQHoAQH4AQKIAgGoAgS4ApSSgPIFwAIB&sid=dcd3ff4c30abfd0bc264d5ae4a4c1d7a&sb=1&sb_lp=1&src=index&src_elem=sb&error_url=https%3A%2F%2Fwww.booking.com%2Findex.html%3Flabel%3Dgen173nr-1FCAEoggI46AdIM1gEaFCIAQGYATG4ARjIAQzYAQHoAQH4AQKIAgGoAgS4ApSSgPIFwAIB%3Bsid%3Ddcd3ff4c30abfd0bc264d5ae4a4c1d7a%3Bsb_price_type%3Dtotal%26%3B&sr_autoscroll=1&ss=hotel+washington&is_ski_area=0&checkin_year=&checkin_month=&checkout_year=&checkout_month=&group_adults=2&group_children=0&no_rooms=1&b_h4u_keep_filters=&from_sf=1

Upvotes: 1

Views: 91

Answers (5)

Muzzamil
Muzzamil

Reputation: 2881

You can locate an element using text only. You can try below solution:

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

element = WebDriverWait(driver, 50).until(
    EC.presence_of_element_located((By.XPATH, "//span[contains(., 'Hotel Washington')]")))
element.click()

Upvotes: 0

nikil reddy
nikil reddy

Reputation: 7

Use the

xPath = //*[contains(@text(),'Hotel Wahington')]

OR

//a[@class='hotel_name_link url']//*[contains(@text(),'Hotel Wahington')]

Upvotes: 0

KunduK
KunduK

Reputation: 33384

To select anchor tag based on text "Hotel Wahington" use the following xpath.

Use following Xpath

driver.find_element_by_xpath("//a[@class='hotel_name_link url' and contains(.,'Hotel Washington')]").click()

Or following css selector.

driver.find_element_by_css_selector("a.hotel_name_link.url[href*='/hotel/nl/washington']").click()

Or partial link text.

driver.find_element_by_partial_link_text("Hotel Washington").click()

Upvotes: 1

Guy
Guy

Reputation: 50819

Using xpath you can go to parent element with ..

driver.find_element_by_xpath('//span[.="Hotel Washington"]/..')

Or locate an element which has specific text

driver.find_element_by_xpath('//a[span[.="Hotel Washington"]]')

Upvotes: 0

Sandeep Koparde
Sandeep Koparde

Reputation: 34

Tryout this xpath: //span[text()='Hotel Washington']/parent::a

Also note that the method "find_element_by_xpath()" in the answers is from version 4 of Selenium which is still in Alpha testing phase. You will be most likely using Selenium version 3. The valid code for version 3 would be:

driver.findElement(By.xpath("//span[text()='Hotel Washington']/parent::a")),click();

Upvotes: 0

Related Questions