无法在Selenium(Python)中点击按钮,尽管元素已正确识别。

huangapple go评论96阅读模式
英文:

Can't click a button in Selenium (Python), even though the element was identified correctly

问题

I am trying to click the Finish button on the following document URL:
https://app.pandadoc.com/p/99be757d50d745395babf924f529d9b7d207b7ec?

And while the finish_button.get_attribute('outerHTML') line outputs the correct answer to find the button (hence it's correctly identified), the program does not returns any errors and also doesn't actually click the button.

print(f"Requesting {document_URL}")
driver.get(document_URL)

print("Waiting for page to load...")
time.sleep(30)

document_detail_page = driver.current_url
print(f'Current page url: {document_detail_page}')

print("Accepting Cookies")
accept_cookies = driver.find_element(By.ID, 'onetrust-accept-btn-handler')
accept_cookies.click()

time.sleep(15)

print("Finding Finish Button to click")
finish_button = driver.find_element(By.XPATH, '//button[text()="Finish"]')
print(f"HTML of button: {finish_button.get_attribute('outerHTML')}")

英文:

I am trying to click the Finish button on the following document URL:
https://app.pandadoc.com/p/99be757d50d745395babf924f529d9b7d207b7ec?

And while the finish_button.get_attribute('outerHTML') line outputs the correct answer to find the button (hence it's correctly identified), the program does not returns any errors and also doesn't actually click the button.

print(f"Requesting {document_URL}")
driver.get(document_URL)

        
print("Waiting for page to load...")
time.sleep(30)

document_detail_page = driver.current_url
print(f'Current page url: {document_detail_page}')

print("Accepting Cookies")
accept_cookies = driver.find_element(By.ID, 'onetrust-accept-btn-handler')
accept_cookies.click()

time.sleep(15)            

print("Finding Finish Button to click")
finish_button = driver.find_element(By.XPATH, '//button[text()="Finish"]')
print(f"HTML of button: {finish_button.get_attribute('outerHTML')}")`

I've tried the following 3 approaches, none of which worked:

finish_button.click()

ActionChains(driver).click(finish_button).perform()

webdriver.ActionChains(driver).move_to_element(finish_button).click(finish_button).perform()

`

EDIT: Someone seems to have clicked the link and hit the button manually (or figured out the solution but didn't post it here). The same goes for the now visible "Download" button that seems to have the same properties.

答案1

得分: 1

我只看到一个 "下载" 按钮。以下是使用 SeleniumBase 点击它并验证下载的Python脚本(在执行 "pip install seleniumbase" 并使用 "python" 运行之后):

from seleniumbase import SB

with SB(browser="chrome", headless=False) as sb:
    sb.open("https://app.pandadoc.com/p/99be757d50d745395babf924f529d9b7d207b7ec")
    sb.click('button[color="primary"]')
    sb.assert_downloaded_file("NDA.pdf")  # 在 ./downloaded_files/ 中搜索
    sb.sleep(5)
英文:

All I see is a Download button. Here's a Python script that clicks it with SeleniumBase (and verifies the download) after pip install seleniumbase and running with python:

from seleniumbase import SB

with SB(browser="chrome", headless=False) as sb:
    sb.open("https://app.pandadoc.com/p/99be757d50d745395babf924f529d9b7d207b7ec")
    sb.click('button[color="primary"]')
    sb.assert_downloaded_file("NDA.pdf")  # Searching ./downloaded_files/
    sb.sleep(5)

huangapple
  • 本文由 发表于 2023年6月8日 07:34:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定