python - 如何通过 Selenium 和 Python 点击​​某个元素

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

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

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>

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

最佳答案

要单击产品页面中的商品以添加到购物车,您可以使用以下任意一项 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()
    

关于python - 如何通过 Selenium 和 Python 点击​​某个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54967544/

相关文章:

python - 我可以从 aws s3 恢复下载吗?

python - 更改 python 打打印新率

python - 在 Azure Functions 中使用 Python 3

c# - 如何通过Selenium和Webdriver提高执行速度

java - 谷歌工具包中的 Xpath 与 selenium webdriver

python - Django 表单字段不会被 POST 数据填充

python - wxPython:如何使 TextCtrl 填充面板

java - Selenium 2 : Open link in new tab and close tabs

Python 结构将 root 密码作为命令行参数传递不起作用

python - 使用 Python 的 Microsoft Graph API 请求 : "Insufficient privileges to complete the operation" error when making simple call