Selenium Click on a Button with Complex HTML Structure
When attempting to click a button with a complex HTML structure using Selenium, you may encounter the NoSuchElementException. This can occur when the button's HTML contains multiple classes or elements with onclick attributes.
To accurately click such buttons, follow these steps:
Remove Spaces in CSS Selectors: When using the CSS selector to locate the element, ensure that there are no spaces between the class names. Correct the following selector:
driver.find_element_by_css_selector('.button .c_button .s_button').click()
To:
driver.find_element_by_css_selector('.button.c_button.s_button').click()
Use Precise CSS Selectors: Construct CSS selectors that target specific elements within the button's HTML. For example:
To click the "Search" button:
driver.find_element_by_css_selector('.s_button span:contains("Search")').click()
To click the "Reset" button:
driver.find_element_by_css_selector('.s_button span:contains("Reset")').click()
By following these steps, you can accurately click buttons with complex HTML structures using Selenium.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3