代码之家  ›  专栏  ›  技术社区  ›  Baktaawar David Maust

在Python web驱动程序中检索HTML元素对象

  •  0
  • Baktaawar David Maust  · 技术社区  · 6 年前

    我正在尝试从我的Splunk帐户自动下载一些csv文件。很遗憾,我们没有API访问权限。

    所以我想用 Selenium Python驱动程序。

    我安装了Selenium并将chrome驱动程序放在正确的路径上。

    现在我想访问的第一个页面是Splunk的登录页面,如下所示

    enter image description here

    import selenium
    from selenium import webdriver
    
    PATH= "https://splunk.com/account/login"
    
    driver = webdriver.Chrome()
    # Open the website
    driver.get(path)
    
    
    username_box = driver.find_element_by_name('username')
    
    # Get the Password box object 
    password_box = driver.find_element_by_name('password')
    
    
    # Send id information for login
    username_box.send_keys(USERNAME)
    password_box.send_keys(PASSWORD)
    
    # Click on the sign in button
    
    sign_in_button=driver.find_element_by_link_text('Sign in')
    sign_in_button.click()
    

    在“登录”按钮驱动程序启动之前,它工作正常。因此,它可以打开页面,从user\u name和password\u元素中输入用户名和密码,但在检索登录元素对象时,它无法检索对象并抛出错误:

    NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Sign in"}
      (Session info: chrome=71.0.3578.98)
    

    下面是我在Chrome上检查时相应登录按钮的HTML

    enter image description here

    所以我先试试

    driver.find_element_by_link_text('Sign in')
    

    driver.find_element_by_class_name('splButton-primary btn') 
    

    但无法检索登录按钮对象。

    因此,它无法单击并登录。

    谢谢

    2 回复  |  直到 6 年前
        1
  •  2
  •   ewwink    6 年前

    Sign in value

    driver.find_element_by_css_selector('input[value="Sign in"]')
    
        2
  •  1
  •   Breaks Software    6 年前

    首先,您的类名包含两个类,请尝试仅使用其中一个:

    driver.find_element_by_class_name('splButton-primary') 
    

    driver.find_element_by_css_selector('input.splButton-primary')