26

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.

6
  • 5
    Well... Have you tried the obvious xpath '//*[@title="Havai 30"]'??? Commented Aug 18, 2014 at 13:09
  • 1
    That worked! With [0] at ending. Was getting list. Commented Aug 18, 2014 at 13:24
  • What do you mean by "I am looking site."? It seems incomprehensible. Commented Nov 10, 2022 at 18:19
  • OK, the OP has left the building: "Last seen more than 7 years ago" Commented Nov 10, 2022 at 18:38
  • "I am looking site." may refer to using the web developer tools in a web browser to look at the HTML source for a web site. For example, "Inspector" in Firefox (menu ToolsBrowser ToolsWeb Developer Tools). Commented Nov 10, 2022 at 18:40

5 Answers 5

36

Like barak manos said, the answer was:

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean by "case it was list"? It seems incomprehensible.
"in case it was a list"?
OK, the OP has left the building: "Last seen more than 7 years ago"
17

Use:

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

This will work for me like you said.

2 Comments

From a comment: "find_element_by_* and find_elements_by_* are removed in Selenium 4.3.0. Use find_element instead.".
It may be something like find_element(By.XPATH, ...), with import "from selenium.webdriver.common.by import By".
6

For Java:

String title = "SOME TITLE";
driver.findElement(By.cssSelector("[title^='" + title + "']")).click();

Comments

1

This works for me:

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

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

Comments

-1

For me it works with the page object pattern:

@FindBy(xpath = "//*[@title='Havai 30']")
WebElement imHavai;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.