简体   繁体   English

Selenium 按钮点击 Python

[英]Selenium button click in Python

Im coding a selenium bot with Python which will be watching videos on different websites.用 Python编写了一个selenium机器人,它将在不同网站上观看视频。 I need it to press button to play video, but it doesnt work.我需要它来按下按钮来播放视频,但它不起作用。 I use Chrome webdriver and tried to use undetected_chromedriver, but nothing changed.我使用 Chrome webdriver 并尝试使用 undetected_chromedriver,但没有任何改变。

My webdriver:我的网络驱动程序:

browser = webdriver.Chrome(ChromeDriverManager().install())

or或者

browser = undetected_chromedriver.Chrome()

My code is:我的代码是:

sendContinue = browser.find_element(By.XPATH,'/html/body/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/a').click()

It worked on login page, but not here.登录页面上有效,但在此处无效。

Button element:按钮元素:

<button class="ytp-large-play-button ytp-button ytp-large-play-button-red-bg" aria-label="Смотреть"><svg height="100%" version="1.1" viewBox="0 0 68 48" width="100%"><path class="ytp-large-play-button-bg" d="M66.52,7.74c-0.78-2.93-2.49-5.41-5.42-6.19C55.79,.13,34,0,34,0S12.21,.13,6.9,1.55 C3.97,2.33,2.27,4.81,1.48,7.74C0.06,13.05,0,24,0,24s0.06,10.95,1.48,16.26c0.78,2.93,2.49,5.41,5.42,6.19 C12.21,47.87,34,48,34,48s21.79-0.13,27.1-1.55c2.93-0.78,4.64-3.26,5.42-6.19C67.94,34.95,68,24,68,24S67.94,13.05,66.52,7.74z" fill="#f00"></path><path d="M 45,24 27,14 27,34" fill="#fff"></path></svg></button>

Can anyone solve this?谁能解决这个问题?

These kind of XPATH are unreliable, could you try the following?这种XPATH不靠谱,可以试试下面的吗?

sendContinue = browser.find_element(By.XPATH,"//*[@class='.ytp-large-play-button.ytp-button']").click()

Off topic, but it sometimes help to wait for the button to appear, like:题外话,但有时等待按钮出现会有所帮助,例如:

def wait_click(driver, xpath, delay = 20):
    try:
        myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, xpath)))
        myElem.click()
        return myElem
    except TimeoutException:
        print("Loading took too much time!")

You can call it with:您可以通过以下方式调用它:

wait_click(driver, "//*[@class='.ytp-large-play-button.ytp-button.ytp-large-play-button-red-bg']")

To interact with any clickable element ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies :要理想地与任何可点击元素交互,您需要为element_to_be_clickable()引入WebDriverWait并且您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.ytp-large-play-button.ytp-button.ytp-large-play-button-red-bg[aria-label='Смотреть'] > svg > path"))).click()
  • Using XPATH :使用XPATH

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ytp-large-play-button ytp-button ytp-large-play-button-red-bg' and @aria-label='Смотреть']"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM