如何在Python中使用selenium单击按钮类

如何在Python中使用selenium单击按钮类,python,css,selenium,automation,selenium-chromedriver,Python,Css,Selenium,Automation,Selenium Chromedriver,有什么建议吗? 谢谢。试试这个 self.driver.find_element_by_css_selector('.w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ') 使用以下定

有什么建议吗? 谢谢。

试试这个

self.driver.find_element_by_css_selector('.w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ')

使用以下定位器来标识元素

Css选择器:

btn = self.driver.find_element_by_css_selector('.w-full h-14 pt-2 pb-1 px-3 bg-accent 
text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big 
focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ')

btn.click()
Xpath:

self.driver.find_element_by_css_selector("div.wrapper >button:nth-of-type(1)").click()
理想情况下,您应该使用
WebDriverWait()
并等待元素可单击

self.driver.find_element_by_xpath("//div[@class='wrapper']/button[1]").click()
您需要导入以下库

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.wrapper >button:nth-of-type(1)"))).click()

我都试过了,但没有一个worked@omzi:您得到了什么错误?对于您建议的CSS选择器,我得到了一个'selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法找到元素:{“方法”:“CSS选择器”,“选择器”:“div.wrapper>按钮:第n个类型(1)”}“@KunduKThe XPath给出了相同的错误webdriverwait给出了超时错误,因为它无法找到它
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.wrapper >button:nth-of-type(1)"))).click()
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.webdriver.support import expected_conditions as EC