我正在尝试通过点击按钮订阅此 reddit 页面。
我已在检查模式下打开页面,找到该按钮并将其 XPATH 复制到下面的代码中。但代码说它找不到该元素。
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
URL = "https://www.reddit.com/user/PBTech"
driver = webdriver.Chrome()
driver.get(URL)
time.sleep(5)
join_Btn = driver.find_element(By.CSS_SELECTOR,"span.button-text")
join_Btn.click()
time.sleep(10)
join_Btn.click()
抛出异常...element not interactable
请让我知道单击
subscribe
按钮的最简单方法...如果可能的话...使用 driver.page_source
或 driver.execute_script("return document.documentElement.outerHTML")
显示整个 HTML ...以便我可以确认订阅按钮已被激活单击,替换为取消订阅按钮,或更改为 subscribed
我将您的确切代码复制到我的 IDE 并运行它。它对我来说工作得很好。代码单击 Follow 按钮,注册弹出窗口将打开。
因为您遇到了
Element not interactable
异常。有两件事值得尝试:
检查以下代码:
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
URL = "https://www.reddit.com/user/PBTech"
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(URL)
wait = WebDriverWait(driver, 10)
join_Btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.button-text")))
join_Btn.click()
time.sleep(10)