簡體   English   中英

Python selenium 點擊按鈕 class 不工作

[英]Python selenium click button by class not working

我正在嘗試單擊帶有 class 的按鈕,但它會引發 ElementNotInteractableException。 這是網站 HTML 代碼

這是我正在使用的代碼

driver = webdriver.Chrome('chromedriver.exe', chrome_options=options)

driver.get('https://physionet.org/lightwave/?db=noneeg/1.0.0')

def get_spo2hr(subject):
    driver.find_element_by_xpath("//select[@name='record']/option[text()='"+subject+"']").click()
    driver.find_element_by_id('ui-id-3').click()
    driver.find_element_by_id('viewann').click()
    driver.find_element_by_id('viewsig').click()
    driver.find_element_by_id('lwform').click()
    driver.find_element_by_css_selector(".fwd").click()
    driver.save_screenshot('screenie.png')
    

get_spo2hr('Subject10_SpO2HR')

當然,在合適的情況下,我總是更喜歡使用他們的 xpath 獲取元素。 話雖如此,我修改了您的代碼以使用其 xpath 找到前進按鈕並且它可以工作。

這是修改后的代碼:

driver = webdriver.Chrome('chromedriver.exe', chrome_options=options)

driver.get('https://physionet.org/lightwave/?db=noneeg/1.0.0')

def get_spo2hr(subject):
    driver.find_element_by_xpath("//select[@name='record']/option[text()='" + subject + "']").click()
    driver.find_element_by_id('ui-id-3').click()
    driver.find_element_by_id('viewann').click()
    driver.find_element_by_id('viewsig').click()
    driver.find_element_by_id('lwform').click()
    driver.find_element_by_xpath('/html/body/div[1]/main/div/div/div/form/div[3]/table/tbody/tr/td[2]/div/button[3]').click()
    driver.save_screenshot('screenie.png')


get_spo2hr('Subject10_SpO2HR')

一件事是(如其他答案所述)不穩定的 css 選擇器更喜歡 xpath

但最主要的是 div 在 dom 渲染時重疊了 a 項只需等待一秒鍾即可等到 dom 加載:

import time
time.sleep(1)

示例代碼:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

driver = webdriver.Chrome()
driver.get('https://physionet.org/lightwave/?db=noneeg/1.0.0')

def get_spo2hr(subject):
    driver.find_element_by_xpath("//select[@name='record']/option[text()='"+subject+"']").click()

    import time
    time.sleep(1)

    driver.find_element_by_id('ui-id-3').click()
    driver.find_element_by_id('viewann').click()
    driver.find_element_by_id('viewsig').click()
    driver.find_element_by_id('lwform').click()
    driver.find_element_by_xpath('/html/body/div[1]/main/div/div/div/form/div[3]/table/tbody/tr/td[2]/div/button[3]').click()
    driver.save_screenshot('screenie.png')

get_spo2hr('Subject10_SpO2HR')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM