Reputation: 2581
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&sid=1b691d9ad57ac7ee7c3d40dac2f7f488&dest_id=-2140479&dest_type=city&group_adults=2&group_children=0&hapos=1&hpos=1&no_rooms=1&sr_order=popularity&srepoch=1581242789&srpvid=87de4712b9a90095&ucfs=1&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>
Upvotes: 1
Views: 91
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
Reputation: 7
Use the
xPath = //*[contains(@text(),'Hotel Wahington')]
OR
//a[@class='hotel_name_link url']//*[contains(@text(),'Hotel Wahington')]
Upvotes: 0
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
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
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