Python硒按类单击按钮不起作用

时间:2020-06-21 17:04:10

标签: python selenium

我试图单击带有其类的按钮,但是它抛出ElementNotInteractableException。 Here is the website HTML code

这是我正在使用的代码

# _typeshed/flask/__init__.pyi

from typing import Any
from flask.ctx import _AppCtxGlobals
from models import User


def __getattr__(name: str) -> Any: ...  # incomplete


class MyGlobals(_AppCtxGlobals):
    user: User
    def __getattr__(self, name: str) -> Any: ...  # incomplete


g: MyGlobals

2 个答案:

答案 0 :(得分:1)

当然,在合适的情况下,我总是更喜欢使用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')

答案 1 :(得分:1)

一件事是(如其他答案所述)不稳定的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')