patriciajlim
patriciajlim

Reputation: 63

Where to find button ID in html inspect?

I am creating a python script to click a button on our online database at work and I am having difficulty finding the button id.
There is no mere 'button id=' and instead just wrapped in a bunch of div classes of the same name. Here is a sample inspection of google when I inspect the search button: inspect google search

from selenium import webdriver

driver = webdriver.Firefox()
# Go to your page url
driver.get('https://www.google.ca/')
# Get button you are going to click by its id ( also you could us find_element_by_css_selector to get element by css selector)
button_element = driver.find_element_by_xpath("//input[@name='btnK']")
button_element.click()

Any insight would be appreciated; I am not familiar with html setups.

Upvotes: 0

Views: 18326

Answers (4)

Mike ASP
Mike ASP

Reputation: 2333

Use either xpath:

(//*[@value='Google Search'])[2]

or

(//*[@aria-label='Google Search'])[2]

to click on 'Google Search' button , Above people are suggesting 'name' attribute but this might break your code. So safe side you can use xpaths suggested by me because those attributes are being used by Google since long time. This text "Google Search" is what people see when they are on Google's home page. So Google might not change soon.

So try to pick attribute cautiously. Enjoy

Upvotes: 1

mkhurmi
mkhurmi

Reputation: 816

Locate it using css Selector or Xpath

driver.find_element_by_css_selector("input[name='btnK']")

or

driver.find_element_by_xpath("//input[@name='btnK']")

If you are using selenium we driver with JavaScript, you can use below code to click on that button.

await driver.findElement(By.css("input[name='btnK']")).click();

Upvotes: 0

terrymorse
terrymorse

Reputation: 7096

Note: This explanation uses JavaScript, since the question tags JavaScript.

The highlighted <input> doesn't have and ID, so it can't be accessed with document.getElementById().

It it does have a name "btnK", so you can use document.querySelector() to access it:

let myInput = document.querySelector('input[name="btnK"]');

This will retrieve the first <input> in the document with name="btnK".

Upvotes: 1

FlatAssembler
FlatAssembler

Reputation: 782

Well, not every HTML element has an ID. If it's not shown in the Developer Tools, it doesn't have it (it's empty string).

Upvotes: 0

Related Questions