Home > Mobile >  How to find webelement when there is no ID in Selenium test script
How to find webelement when there is no ID in Selenium test script

Time:01-07

I have a button displayed on the browser with the following tag which doesn't have ID or a single CSS class name. How can I find the Webelement and add a click to this button.

<button  aria-describedby="notes" aria-label="Notes" autofocus=""  role="dialog" type="button">OK</button>

CodePudding user response:

Consider locating the Element by Tag Name. Go to the parent element which has a id or class name and get subelements by tag name (parentelement.find_element_by_tag_name('button')) which is your button

Besides that, the button in your example has two classes.

You click on a button via the .click() command.

CodePudding user response:

Web elements can be located by XPath or CSS Selectors based on any unique combination of any attribute values or / and tag names and also based on their parent or child elements.
Since you didn't share the entire HTML of that page we can only guess about the correct unique locator for that element.
With XPath it can be something like:

"//button[text()='OK']"

Or

"//button[@aria-label='Notes']"

In CSS Selector style it will be

"button[aria-label='Notes']"
  •  Tags:  
  • Related