2

I want to automatically click on an element in a web page using the following code in Python:

driver.find_element_by_id("book appointment").click()

The web page snapshot is as follows:

Enter image description here

My problem is that I cannot find "Book Appointment" text in the page source to extract its ID. How can I find its ID?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hossein
  • 2,041
  • 1
  • 16
  • 29
  • 6
    Use chrome dev tools or the firebug firefox extension to inspect the element. In chrome, just right click the element and select `inspect`. It will take you to the element in the dom. Then you can see if that element has an `ID`, or if you'll have to identify it with some other attribute. If this is enough to answer your question let me know and I'll post it as an answer. – mrfreester Apr 05 '17 at 16:00
  • 1
    we need to see the page source code – Moe Ghafari Apr 05 '17 at 16:00
  • Can you add the html code of that button which you want to locate – NarendraR Apr 05 '17 at 16:18
  • @mrfreester Thanks. This is what I get from inspecting that element: `  Book Appointment` What attribute can I use to identify this element? – Hossein Apr 05 '17 at 16:18
  • And if this element can be obtained using `inspect`, why it is not available in the page source? – Hossein Apr 05 '17 at 16:20
  • This button doesn't have an `id` attribute, based on the code you've provided. You'll have to iterate over the class `"btn btn-inverse btn-spacer"` and check for the known inner text `"  Book Appointment"` before returning the button object which you can click, or using an XPATH specification. – David Zemens Apr 05 '17 at 16:28
  • For such questions: There is [a difference](https://stackoverflow.com/questions/50610553) between timing problems and dynamic content (an element may or may not be present, e.g. depending on input data): *"There are two cases to account for: ... 1) Is the element* ***present***; *meaning does it exist in the DOM. 2) Is the element* ***visible***; *meaning it is in DOM and does not have a hidden or equivalent flag.""* – Peter Mortensen Nov 13 '22 at 18:22

2 Answers2

3

The HTML code provided by you doesn't contains any Id attribute for this element, so it is not possible to locate the element by ID. Alternatively, you can locate the element with text using an XPath selector. Try the below code to locate the same:

driver.find_element_by_xpath("//a[contains(.,'Book Appointment')]")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • 1
    Thanks. But this code snippet raise the following error: `selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //a[contains(.,'Book Appointment')]` – Hossein Apr 05 '17 at 16:46
  • 2
    I found out what was the problem. I should wait until the page is completely loaded. – Hossein Apr 05 '17 at 17:10
2

find_element_by_id needs an id as input. You are giving text content as input.

You may want to try some different selector. Maybe find_element_by_link_text.

There are various strategies to locate elements in a page.

Methods to locate elements in a page:

  • find_element_by_id
  • find_element_by_name
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

To find multiple elements

  • find_elements_by_name
  • find_elements_by_xpath
  • find_elements_by_link_text
  • find_elements_by_partial_link_text
  • find_elements_by_tag_name
  • find_elements_by_class_name
  • find_elements_by_css_selector

For a reference on each selector, you can refer to Locating Elements.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hara
  • 1,467
  • 4
  • 18
  • 35
  • Yes, I gave '"book appointment" just as in indicator. I tried `find_element_by_link_text` but it did not work too. – Hossein Apr 05 '17 at 16:08
  • 1
    @Hossein the link text is *not* `"book appointment"`, rather, it is `"  Book Appointment"`. – David Zemens Apr 05 '17 at 16:32
  • @Hossein did you try with xpath? it should work. first try with fixed xpath and then try to make your xpath generic. Again it depends on the webpage. – Hara Apr 05 '17 at 16:51
  • I tried xpath according to the answer by @NarendraRajput but as I commented there, it did not work. And I don't know what is the reason. – Hossein Apr 05 '17 at 17:00
  • They have [apparently been deprecated](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."*. Though it doesn't really answer the question what can be done if the number of elements is different from exactly one. – Peter Mortensen Nov 13 '22 at 18:08