单击运行时关闭,(selenium)

xvw2m8pv  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(130)

当单击运行时关闭。
我想只是点击按钮。我想做点击xpath。但它不工作。它关闭没有点击。所以它打开边缘。等待等待和关闭。它没有运行我的点击命令。并有一个错误。它的“webdriver对象没有属性find_element_by_xpath”

from selenium import webdriver
import time
driver = webdriver.Edge()
driver.get("https://www.instagram.com/")

time.sleep(7)

log_in = driver.find_element_by_xpath("//*[@id='mount_0_0_+h']/div/div/div/div[1]/div/div/div/div[1]/section/main/article/div[2]/div[2]/div/p/a/span")
log_in.click()

driver.close()
brgchamk

brgchamk1#

我想这就是你想要的:

# Needed libs
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

#Open the browser
driver = webdriver.Edge()
driver.get("https://www.instagram.com/")

# Wait and click for allow cookies button
allow_cookies_button = date = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f"//button[text()='Only allow essential cookies']")))
allow_cookies_button.click()
# Wait and click for Sign in button
signup_button = date = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f"//span[text()='Sign up']")))
signup_button.click()

# This time.sleep is to allow you to see what happened, you can delete it
time.sleep(5)
# Close the browser
driver.close()

为什么你的代码不工作?因为正如你的控制台告诉你的,“find_element_by_xpath”不是驱动程序的有效方法/属性。
自动化/刮削建议:

  • 等待您将使用的元素(无论您想写、单击还是其他操作,都要确保该元素在屏幕上)
  • 尝试使用可理解的xpath(或任何选择器),您将在将来重用它们。您更了解什么,//span[text()='Sign up']还是//*[@id='mount_0_0_+h']/div/div/div/div[1]/div/div/div/div[1]/section/main/article/div[2]/div[2]/div/p/a/span?好的选择器将在将来保存大量时间

相关问题