【问题标题】:How to click on an element through Selenium and Python如何通过 Selenium 和 Python 点击​​元素
【发布时间】:2019-03-03 09:47:14
【问题描述】:

我正在尝试单击产品页面中的某个项目以将某些内容添加到我的购物车中,但我无法执行此操作,因为我遇到了很多错误或没有任何反应。 这是我的代码:

i = driver.find_element_by_xpath("//button[@class='exclusive']")
i.click

这是网页代码:

<p id="add_to_cart" class="buttons_bottom_block no-print">
  <button type="submit" name="Submit" class="exclusive">
    <span>Add to cart</span>
    </button>
</p>

对不起,如果我犯了错误,但我是新人!感谢您的帮助。

【问题讨论】:

  • i.click()。更正这可能是您的语法错误
  • 我遇到很多错误,哪些是?将它们添加到问题中。

标签: python python-3.x selenium-webdriver xpath css-selectors


【解决方案1】:

要点击产品页面中的商品添加到购物车,您可以使用以下任一Locator Strategies

  • 使用css_selector

    driver.find_element_by_css_selector("p.buttons_bottom_block.no-print>button.exclusive[name='Submit']>span").click()
    
  • 使用xpath

    driver.find_element_by_xpath("//p[@class='buttons_bottom_block no-print']/button[@class='exclusive']/span[text()='Add to cart']").click()
    

【讨论】:

    猜你喜欢
    • 2019-05-10
    • 1970-01-01
    • 2019-06-09
    • 2022-01-17
    • 2020-04-09
    • 2020-11-06
    • 2020-04-28
    • 2018-08-16
    • 1970-01-01
    相关资源
    最近更新 更多