使用 selenium Python 查找正确的 xpath

发布于 2025-01-13 09:13:21 字数 419 浏览 2 评论 0 原文

我目前正在开发一个使用 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 吗?

I'm currently working on a project with Selenium and webdriver. So far so good I can navigate through the website using selenium without any problem however, I would need to click on a specific icon to slides through to a date and time table. Issue is when looking at the HTML I have no idea what to locate with Selenium in order to click on that icon.

HTML sample:

<a _ngcontent-oyt-c155="" class="btn btn-soft-primary day-selector-navbutton"><i _ngcontent-oyt-c155="" class="fa fa-chevron-right"></i></a>

Usually I'm locating items by name, ID or Class but in that case I don't know where to got with.

Should I look for the xpath instead ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶ ̄淡然 2025-01-20 09:13:21

根据您在这里分享的内容,这个 XPath 应该可以工作:

'//a[@class="btn btn-soft-primary day-selector-navbutton"]//i[@class="fa fa-chevron-right"]'

我不能完全确定这个定位器是否唯一或者是否没有 iframe 等。
您可能还需要在这里添加某种等待。如果是这样,您最好使用预期条件显式等待。

Accordingly to what you have shared here this XPath should work:

'//a[@class="btn btn-soft-primary day-selector-navbutton"]//i[@class="fa fa-chevron-right"]'

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.

盗心人 2025-01-20 09:13:21

所需的元素是 Angular 元素,因此 click() 需要诱发可点击元素href="https://stackoverflow.com/a/59130336/7429447">WebDriverWait 的 element_to_be_clickable( ) 并且您可以使用以下任一定位器策略

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-soft-primary.day-selector-navbutton > i.fa.fa-chevron-右"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-soft-primary day-selector-navbutton']/i[@ class='fa fa-chevron-right']"))).click()
    
  • 注意:您必须添加以下导入:

    从 selenium.webdriver.support.ui 导入 WebDriverWait
    从 selenium.webdriver.common.by 导入
    从 selenium.webdriver.support 导入预期条件作为 EC
    

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:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-soft-primary.day-selector-navbutton > i.fa.fa-chevron-right"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-soft-primary day-selector-navbutton']/i[@class='fa fa-chevron-right']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文