Skip to content

如果通过selenium控制Web页面,并模拟单击按钮的动作 #126

@Sogrey

Description

@Sogrey
from selenium import webdriver
import time

browser = webdriver.Chrome('./webdriver/chromedriver')

try:
    browser.get('http://localhost/demo.html')

    buttons = browser.find_elements_by_class_name('mybutton')
    i = 0
    while True:
        buttons[i].click()
        time.sleep(1)
        i += 1
        if i == len(buttons):
            i = 0


except Exception as e:
    print(e)
    browser.close()

demo.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>彩色按钮</title>
</head>
<body>
    <script>

        function onclick_color(e) {
            document.getElementById("bgcolor").style.background = e.style.background
        }
    </script>

  <button class="mybutton" style="background: red" onclick="onclick_color(this)">按钮1</button>
  <button class="mybutton" style="background: blue" onclick="onclick_color(this)">按钮2</button>
  <button class="mybutton" style="background: yellow" onclick="onclick_color(this)">按钮3</button>
  <br>
  <button class="mybutton" style="background: green" onclick="onclick_color(this)">按钮4</button>
  <button class="mybutton" style="background: blueviolet" onclick="onclick_color(this)">按钮5</button>
  <button class="mybutton" style="background: gold" onclick="onclick_color(this)">按钮6</button>
  <p></p>
  <div id="bgcolor" style="width: 200px; height: 200px">


  </div>
</body>
</html>

chromedriver

模拟单击按钮的动作,关键是先找到要单击的按钮,然后之间调用click方法即可。

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions