25

I am looking site. In the inspect element, see this:

<span id="item60" title="Havai 30" class="item button link">Get</span>
<span id="item90" title="Classic 50" class="item button link">Get</span>

I need to get and click an element by title. Something like this:

browser.find_element_by_xpath('//*[@id="item60"]').click()

But via title.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cask
  • 747
  • 1
  • 8
  • 13

5 Answers5

33

Like barak manos said, the answer was:

'//*[@title="Havai 30"]'

With [0] at ending, case it was list.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cask
  • 747
  • 1
  • 8
  • 13
17

Use:

browser.find_element_by_xpath('//*[@title="Havai 30"]').click()

This will work for me like you said.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arun V Jose
  • 3,321
  • 2
  • 24
  • 24
  • From [a comment](https://stackoverflow.com/questions/30002313/selenium-finding-elements-by-class-name-in-python#comment128785684_30025430): *"`find_element_by_*` and `find_elements_by_*` are removed in Selenium 4.3.0. Use `find_element` instead."*. – Peter Mortensen Nov 10 '22 at 18:31
  • It may be [something like](https://stackoverflow.com/questions/73056864/selenium-by-xpath-what-to-import/73057414#73057414) `find_element(By.XPATH, ...)`, with import "`from selenium.webdriver.common.by import By`". – Peter Mortensen Nov 10 '22 at 18:33
5

For Java:

String title = "SOME TITLE";
driver.findElement(By.cssSelector("[title^='" + title + "']")).click();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
xMilos
  • 1,519
  • 4
  • 21
  • 36
1

This works for me:

driver.find_element_by_xpath('//*[@title="Havai 30"]').click()

** Make sure you include the starting and ending square brackets!

RF1991
  • 2,037
  • 4
  • 8
  • 17
-1

For me it works with the page object pattern:

@FindBy(xpath = "//*[@title='Havai 30']")
WebElement imHavai;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131