最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Where to find button ID in html inspect? - Stack Overflow

programmeradmin8浏览0评论

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:

from selenium import webdriver

driver = webdriver.Firefox()
# Go to your page url
driver.get('/')
# 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.

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:

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.

Share Improve this question edited May 9, 2020 at 19:08 patriciajlim asked May 7, 2020 at 18:19 patriciajlimpatriciajlim 632 gold badges3 silver badges12 bronze badges 3
  • 1 there is not button id added, instead you can use 'name' or 'class' attribute. – Hamza Arshad Commented May 7, 2020 at 18:22
  • 1 I can see <input name="btnI"........................ type="submit"... /> . This is a button in your website. Please refer to this link.developer.mozilla/en-US/docs/Web/HTML/Element/input/button – Teja Swaroop Arukoti Commented May 7, 2020 at 18:23
  • In python and selenium, you would input the button element id, can I input this instead? – patriciajlim Commented May 7, 2020 at 20:24
Add a ment  | 

4 Answers 4

Reset to default 1

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

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".

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).

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();
发布评论

评论列表(0)

  1. 暂无评论