简体   繁体   English

Python selenium 点击按钮 class 不工作

[英]Python selenium click button by class not working

I'm trying to click a button with its class but it throws an ElementNotInteractableException.我正在尝试单击带有 class 的按钮,但它会引发 ElementNotInteractableException。 Here is the website HTML code这是网站 HTML 代码

Here is the code I'm using这是我正在使用的代码

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')

I always prefer getting elements using their xpath, of course, in suitable situations.当然,在合适的情况下,我总是更喜欢使用他们的 xpath 获取元素。 With that being said, I modified your code to find the forward button using its xpath and it works.话虽如此,我修改了您的代码以使用其 xpath 找到前进按钮并且它可以工作。

Here is the modified code:这是修改后的代码:

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')

One thing is (as said in other answers) the unstable css selector prefer xpath一件事是(如其他答案所述)不稳定的 css 选择器更喜欢 xpath

But the main thing is that the div is overlapping the a item at the dom rendering Just wait one second to wait until the dom loads:但最主要的是 div 在 dom 渲染时重叠了 a 项只需等待一秒钟即可等到 dom 加载:

import time
time.sleep(1)

Example code:示例代码:

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