Python/Selenium -无法单击www.example.com上的“接受cookie”按钮www.instagram.com

fruv7luv  于 2023-05-29  发布在  Python
关注(0)|答案(4)|浏览(178)

我尝试使用python selenium登录instagram。但我必须接受cookie才能继续。

这是我的密码

  1. class InstaBot:
  2. def __init__(self, username, pw):
  3. self.driver = webdriver.Chrome()
  4. self.driver.get('https://www.instagram.com/')
  5. sleep(2)
  6. #this is the code that im trying to use, so to click the accept button
  7. self.driver.find_element_by_xpath("/html/body/div[2]/div/div/div/div[2]/button[1]").click()
  8. self.driver.find_element_by_xpath("//input[@name=\"username\"]")\
  9. .send_keys(username)
  10. self.driver.find_element_by_xpath("//input[@name=\"password\"]")\
  11. .send_keys(pw)
  12. self.driver.find_element_by_xpath("//a[contains(text(), 'Log in')]")\
  13. .click()
  14. sleep(4)

问题是,当它点击接受按钮时,它什么也不做。有什么想法吗

aamkag61

aamkag611#

应单击accept the login及其后面的两个元素。只要等到元素变成可点击的,然后点击它。

  1. WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept']"))).click()
  2. #Your code
  3. WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#loginForm > div > div:nth-child(3) > button"))).click()
  4. WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Save Info']"))).click()
  5. WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Turn On']"))).click()

进口

  1. from selenium.webdriver.common.by import By
  2. from selenium.webdriver.support.ui import WebDriverWait
  3. from selenium.webdriver.support import expected_conditions as EC
uhry853o

uhry853o2#

试试这个:

  1. self.driver.find_element_by_xpath("//button[text()='Accept']").click()

我在这里发布了我的解决方案:在www.example.com上使用Python/Selenium接受cookie错误www.instagram.com

j8ag8udp

j8ag8udp3#

你可以使用这个代码:

  1. from selenium.webdriver.common.by import By
  2. from selenium.webdriver.support.wait import WebDriverWait
  3. from selenium.webdriver import Chrome
  4. from selenium import webdriver
  5. alert=WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Accept All")]'))).click()
o2rvlv0m

o2rvlv0m4#

我找到了一种方法,通过选择相应文本的按钮。

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. cookie_button = WebDriverWait(wd,15).until(
  6. EC.element_to_be_clickable(
  7. (By.XPATH,
  8. "//button[text()='Accept all cookies']")))
  9. cookie_button.click()

相关问题