Selenium 脚本来检测按钮的存在并在它看起来不起作用时单击它
Selenium script to detect presence of button and to click on it when it appears not working
Python/coding新人,小心!
我正在编写脚本以从“https://ytmp3.cc/en13/”的 YouTube 下载。我已经使用 pyautogui 编写了一个点击脚本,但问题是下载按钮出现在输入 link 的 1 到 15 秒之间的任何地方。所以我想在 Selenium window 中重新编码以动态等待直到按钮可见,然后继续单击脚本。我已经尝试了大约 15 种方法,但无法正常工作
图书馆废话:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
我尝试让它工作的方式:
没有等待,导致 NosuchElement 异常
def check_exists():
try:
while True:
a=driver.find_element(By.LINK_TEXT, "Download")
a.click()
except NoSuchElementException:
time.sleep(10)
这个语法很可能是错误的
我尝试了隐式和显式等待的几种不同变体,但无法让它工作。 link 网站上是否有一些 javascript 上面的内容扰乱了我的代码?
在单独的 .py 中,运行
#driver.find_element(By.LINK_TEXT, "Download").click()
#either the line above or below works at finding the element, and clicking it
driver.find_element_by_link_text("Download").click()
我只需要帮助让上面的行 ^ 进入显式等待,但我还不具备如何做的语言知识。我会很感激一些帮助,提前致谢!
您可以使用 webdriver wait 等待按钮显示。尝试了以下对我有用的代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
video_path = "your_video_path"
waitTime=10
driver = webdriver.Chrome("./chromedriver.exe")
driver.get("https://ytmp3.cc/")
element = driver.find_element_by_id("input")
element.send_keys(video_path)
driver.find_element_by_id("submit").click()
try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, waitTime).until(
EC.presence_of_element_located((By.LINK_TEXT,"Download"))
)
driver.find_element_by_link_text("Download").click()
finally:
print(driver.title)
您可以在 waitTime
变量中以秒为单位更新您所需的等待时间,因此如果值为 10,selenium 将等待并检查最多 10 秒的元素
Python/coding新人,小心!
我正在编写脚本以从“https://ytmp3.cc/en13/”的 YouTube 下载。我已经使用 pyautogui 编写了一个点击脚本,但问题是下载按钮出现在输入 link 的 1 到 15 秒之间的任何地方。所以我想在 Selenium window 中重新编码以动态等待直到按钮可见,然后继续单击脚本。我已经尝试了大约 15 种方法,但无法正常工作
图书馆废话:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
我尝试让它工作的方式:
没有等待,导致 NosuchElement 异常
def check_exists():
try:
while True:
a=driver.find_element(By.LINK_TEXT, "Download")
a.click()
except NoSuchElementException:
time.sleep(10)
这个语法很可能是错误的 我尝试了隐式和显式等待的几种不同变体,但无法让它工作。 link 网站上是否有一些 javascript 上面的内容扰乱了我的代码?
在单独的 .py 中,运行
#driver.find_element(By.LINK_TEXT, "Download").click()
#either the line above or below works at finding the element, and clicking it
driver.find_element_by_link_text("Download").click()
我只需要帮助让上面的行 ^ 进入显式等待,但我还不具备如何做的语言知识。我会很感激一些帮助,提前致谢!
您可以使用 webdriver wait 等待按钮显示。尝试了以下对我有用的代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
video_path = "your_video_path"
waitTime=10
driver = webdriver.Chrome("./chromedriver.exe")
driver.get("https://ytmp3.cc/")
element = driver.find_element_by_id("input")
element.send_keys(video_path)
driver.find_element_by_id("submit").click()
try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, waitTime).until(
EC.presence_of_element_located((By.LINK_TEXT,"Download"))
)
driver.find_element_by_link_text("Download").click()
finally:
print(driver.title)
您可以在 waitTime
变量中以秒为单位更新您所需的等待时间,因此如果值为 10,selenium 将等待并检查最多 10 秒的元素