简体   繁体   中英

Python selenium click button by class not working

I'm trying to click a button with its class but it throws an ElementNotInteractableException. Here is the website HTML code

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. With that being said, I modified your code to find the forward button using its xpath and it works.

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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