使用 selenium Python 查找正确的 xpath
我目前正在开发一个使用 Selenium 和 webdriver 的项目。到目前为止,我可以使用 Selenium 浏览网站,没有任何问题,但是,我需要单击特定的图标才能滑动到日期和时间表。问题是,在查看 HTML 时,我不知道要使用 Selenium 找到什么才能单击该图标。
HTML 示例:
<a _ngcontent-oyt-c155="" class="btn btn-soft-primary day-selector-navbutton"><i _ngcontent-oyt-c155="" class="fa fa-chevron-right"></i></a>
通常我会按名称、ID 或类来查找项目,但在这种情况下我不知道从哪里获取。
我应该寻找 xpath 吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您在这里分享的内容,这个 XPath 应该可以工作:
我不能完全确定这个定位器是否唯一或者是否没有 iframe 等。
您可能还需要在这里添加某种等待。如果是这样,您最好使用预期条件显式等待。
Accordingly to what you have shared here this XPath should work:
I can't be completely sure if this locator is unique or if there is no iframe there etc.
You will possibly need to add some kind of wait here too. If so you should preferably use Expected Conditions explicit waits.
所需的元素是 Angular 元素,因此 click() 需要诱发可点击元素href="https://stackoverflow.com/a/59130336/7429447">WebDriverWait 的 element_to_be_clickable( ) 并且您可以使用以下任一定位器策略:
使用CSS_SELECTOR:
使用XPATH:
注意:您必须添加以下导入:
The desired element is a Angular element, so to click() on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
Using XPATH:
Note: You have to add the following imports :