【发布时间】:2019-05-15 13:36:49
【问题描述】:
我正在使用空闲和硒。 这是我的代码:
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome()
driver.get('https://www.youtube.com/')
def check_exist_by_class():
try:
driver.find_element_by_class_name("ytp-ad-skip-button ytp-button")
#time.sleep(1)
return 0
except NoSuchElementException:
#time.sleep(1)
return 1
while True:
print(check_exist_by_class())
time.sleep(0.5)
即使跳过添加按钮可见,我也只会得到 1。尝试使用 x 路径,但按钮的 x 路径会随着窗口大小而变化。
更新- X 路径不会随窗口侧而改变。它是随机的。知道如何使用 selenium 单击跳过添加按钮吗?
几个广告按钮的X个路径:
***//*[@id="skip-button:3f"]/span/button
//*[@id="skip-button:2v"]/span/button
//*[@id="skip-button:2v"]/span/button
//*[@id="skip-button:a"]/span/button
//*[@id="skip-button:a"]/span/button
//*[@id="skip-button:a"]/span/button
//*[@id="skip-button:2b"]/span/button***
【问题讨论】:
标签: python-3.x automation youtube selenium-chromedriver