基于Selenium+Python实现自动解数独,秒变最强大脑

简介: 基于Selenium+Python实现自动解数独,秒变最强大脑

前言

在本文中,我将向大家介绍如何使用Python编写一个数独脚本,并实现模拟完成数独游戏的脚本。数独是一种经典的逻辑游戏,它可以帮助我们锻炼逻辑思维能力。通过编写数独脚本,我们可以使用Python语言解决数独问题,从而更好地理解编程和算法。

效果

环境使用

  • python 3.9
  • pycharm

模块使用

  • selenium

模块介绍

  • requests

       requests是一个很实用的Python HTTP客户端库,爬虫和测试服务器响应数据时经常会用到,requests是Python语言的第三方的库,专门用于发送HTTP请求,使用起来比urllib简洁很多。

  • parsel

       parsel是一个python的第三方库,相当于css选择器+xpath+re。

parsel由scrapy团队开发,是将scrapy中的parsel独立抽取出来的,可以轻松解析html,xml内容,获取需要的数据。

相比于BeautifulSoup,xpath,parsel效率更高,使用更简单。

  • re

       re模块是python独有的匹配字符串的模块,该模块中提供的很多功能是基于正则表达式实现的,而正则表达式是对字符串进行模糊匹配,提取自己需要的字符串部分,他对所有的语言都通用。

  • os

       os 就是 “operating system” 的缩写,顾名思义,os模块提供的就是各种 Python 程序与操作系统进行交互的接口。通过使用 os 模块,一方面可以方便地与操作系统进行交互,另一方面也可以极大增强代码的可移植性。

  • csv

       它是一种文件格式,一般也被叫做逗号分隔值文件,可以使用 Excel 软件或者文本文档打开 。其中数据字段用半角逗号间隔(也可以使用其它字符),使用 Excel 打开时,逗号会被转换为分隔符。csv 文件是以纯文本形式存储了表格数据,并且在兼容各个操作系统。

模块安装问题:

  • 如果安装python第三方模块:

win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车

在pycharm中点击Terminal(终端) 输入安装命令

  • 安装失败原因:
  • 失败一: pip 不是内部命令

               解决方法: 设置环境变量

  • 失败二: 出现大量报红 (read time out)

               解决方法: 因为是网络链接超时, 需要切换镜像源

 

    清华:https://pypi.tuna.tsinghua.edu.cn/simple
    阿里云:https://mirrors.aliyun.com/pypi/simple/
    中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
    华中理工大学:https://pypi.hustunique.com/
    山东理工大学:https://pypi.sdutlinux.org/
    豆瓣:https://pypi.douban.com/simple/
    例如:pip3 install -i https://pypi.doubanio.com/simple/ 模块名

失败三: cmd里面显示已经安装过了, 或者安装成功了, 但是在pycharm里面还是无法导入

               解决方法: 可能安装了多个python版本 (anaconda 或者 python 安装一个即可) 卸载一个就好,或者你pycharm里面python解释器没有设置好。

代码实现

这个是不是很厉害,实际上很简单,接下来,我一步一步教大家如何完成这样的脚本。

获取数独数据

我们首先进入游戏的网页,把网页中的九宫格的数字获取下来。在线数独游戏官网


接下来,我们使用开发者工具,获取这些数字的CSS或者XPath,因为我们要用selenium获取下来,这里我们要注意,我们这里还要注意数字的顺序,否则,会影响我们后面解数独。我们先看看网页源代码吧。


我们会发现,每九个数放在了一个class="sz?"里面,其中每个数都有自己对应的坐标,比如说,第一行第一列的数就是c_0_0,第一个0表示列数,第二个0表示行数,这个对我们后期代码的编写有很大的帮助。下面,我们先写一个获取第一行第一列的数的代码。

    c_0_0 = driver.find_element(By.CSS_SELECTOR, '#c_0_0').text
    if c_0_0 == '':
        c_0_0 = 0
        list1.append(c_0_0)
    else:
        list1.append(int(c_0_0))


这段代码使用了Selenium库中的find_element方法来查找一个元素,并获取该元素的文本。然后,它检查这个文本是否为空,如果为空,则将空文本转换为整数0,并将其添加到一个名为list1的列表中。如果文本不为空,则将其转换为整数,并添加到list1列表中。至于为什么要放到列表里面呢,是因为后面我们解数独要要用到,其他数的代码都一样,我这里就直接展示了。

list1 = []
    # (0,0)
    c_0_0 = driver.find_element(By.CSS_SELECTOR, '#c_0_0').text
    if c_0_0 == '':
        c_0_0 = 0
        list1.append(c_0_0)
    else:
        list1.append(int(c_0_0))
    # (1,0)
    c_1_0 = driver.find_element(By.CSS_SELECTOR, '#c_1_0').text
    if c_1_0 == '':
        c_1_0 = 0
        list1.append(c_1_0)
    else:
        list1.append(int(c_1_0))
    # (2,0)
    c_2_0 = driver.find_element(By.CSS_SELECTOR, '#c_2_0').text
    if c_2_0 == '':
        c_2_0 = 0
        list1.append(c_2_0)
    else:
        list1.append(int(c_2_0))
    # (3,0)
    c_3_0 = driver.find_element(By.CSS_SELECTOR, '#c_3_0').text
    if c_3_0 == '':
        c_3_0 = 0
        list1.append(c_3_0)
    else:
        list1.append(int(c_3_0))
    # (4,0)
    c_4_0 = driver.find_element(By.CSS_SELECTOR, '#c_4_0').text
    if c_4_0 == '':
        c_4_0 = 0
        list1.append(c_4_0)
    else:
        list1.append(int(c_4_0))
    # (5,0)
    c_5_0 = driver.find_element(By.CSS_SELECTOR, '#c_5_0').text
    if c_5_0 == '':
        c_5_0 = 0
        list1.append(c_5_0)
    else:
        list1.append(int(c_5_0))
    # (6,0)
    c_6_0 = driver.find_element(By.CSS_SELECTOR, '#c_6_0').text
    if c_6_0 == '':
        c_6_0 = 0
        list1.append(c_6_0)
    else:
        list1.append(int(c_6_0))
    # (7,0)
    c_7_0 = driver.find_element(By.CSS_SELECTOR, '#c_7_0').text
    if c_7_0 == '':
        c_7_0 = 0
        list1.append(c_7_0)
    else:
        list1.append(int(c_7_0))
    # (8,0)
    c_8_0 = driver.find_element(By.CSS_SELECTOR, '#c_8_0').text
    if c_8_0 == '':
        c_8_0 = 0
        list1.append(c_8_0)
    else:
        list1.append(int(c_8_0))
    list2 = []
    # (0,1)
    c_0_1 = driver.find_element(By.CSS_SELECTOR, '#c_0_1').text
    if c_0_1 == '':
        c_0_1 = 0
        list2.append(c_0_1)
    else:
        list2.append(int(c_0_1))
    # (1,1)
    c_1_1 = driver.find_element(By.CSS_SELECTOR, '#c_1_1').text
    if c_1_1 == '':
        c_1_1 = 0
        list2.append(c_1_1)
    else:
        list2.append(int(c_1_1))
    # (2,1)
    c_2_1 = driver.find_element(By.CSS_SELECTOR, '#c_2_1').text
    if c_2_1 == '':
        c_2_1 = 0
        list2.append(c_2_1)
    else:
        list2.append(int(c_2_1))
    # (3,1)
    c_3_1 = driver.find_element(By.CSS_SELECTOR, '#c_3_1').text
    if c_3_1 == '':
        c_3_1 = 0
        list2.append(c_3_1)
    else:
        list2.append(int(c_3_1))
    # (4,1)
    c_4_1 = driver.find_element(By.CSS_SELECTOR, '#c_4_1').text
    if c_4_1 == '':
        c_4_1 = 0
        list2.append(c_4_1)
    else:
        list2.append(int(c_4_1))
    # (5,1)
    c_5_1 = driver.find_element(By.CSS_SELECTOR, '#c_5_1').text
    if c_5_1 == '':
        c_5_1 = 0
        list2.append(c_5_1)
    else:
        list2.append(int(c_5_1))
    # (6,1)
    c_6_1 = driver.find_element(By.CSS_SELECTOR, '#c_6_1').text
    if c_6_1 == '':
        c_6_1 = 0
        list2.append(c_6_1)
    else:
        list2.append(int(c_6_1))
    # (7,1)
    c_7_1 = driver.find_element(By.CSS_SELECTOR, '#c_7_1').text
    if c_7_1 == '':
        c_7_1 = 0
        list2.append(c_7_1)
    else:
        list2.append(int(c_7_1))
    # (8,1)
    c_8_1 = driver.find_element(By.CSS_SELECTOR, '#c_8_1').text
    if c_8_1 == '':
        c_8_1 = 0
        list2.append(c_8_1)
    else:
        list2.append(int(c_8_1))
    list3 = []
    # (0,2)
    c_0_2 = driver.find_element(By.CSS_SELECTOR, '#c_0_2').text
    if c_0_2 == '':
        c_0_2 = 0
        list3.append(c_0_2)
    else:
        list3.append(int(c_0_2))
    # (1,2)
    c_1_2 = driver.find_element(By.CSS_SELECTOR, '#c_1_2').text
    if c_1_2 == '':
        c_1_2 = 0
        list3.append(c_1_2)
    else:
        list3.append(int(c_1_2))
    # (2,2)
    c_2_2 = driver.find_element(By.CSS_SELECTOR, '#c_2_2').text
    if c_2_2 == '':
        c_2_2 = 0
        list3.append(c_2_2)
    else:
        list3.append(int(c_2_2))
    # (3,2)
    c_3_2 = driver.find_element(By.CSS_SELECTOR, '#c_3_2').text
    if c_3_2 == '':
        c_3_2 = 0
        list3.append(c_3_2)
    else:
        list3.append(int(c_3_2))
    # (4,2)
    c_4_2 = driver.find_element(By.CSS_SELECTOR, '#c_4_2').text
    if c_4_2 == '':
        c_4_2 = 0
        list3.append(c_4_2)
    else:
        list3.append(int(c_4_2))
    # (5,2)
    c_5_2 = driver.find_element(By.CSS_SELECTOR, '#c_5_2').text
    if c_5_2 == '':
        c_5_2 = 0
        list3.append(c_5_2)
    else:
        list3.append(int(c_5_2))
    # (6,2)
    c_6_2 = driver.find_element(By.CSS_SELECTOR, '#c_6_2').text
    if c_6_2 == '':
        c_6_2 = 0
        list3.append(c_6_2)
    else:
        list3.append(int(c_6_2))
    # (7,2)
    c_7_2 = driver.find_element(By.CSS_SELECTOR, '#c_7_2').text
    if c_7_2 == '':
        c_7_2 = 0
        list3.append(c_7_2)
    else:
        list3.append(int(c_7_2))
    # (8,2)
    c_8_2 = driver.find_element(By.CSS_SELECTOR, '#c_8_2').text
    if c_8_2 == '':
        c_8_2 = 0
        list3.append(c_8_2)
    else:
        list3.append(int(c_8_2))
    list4 = []
    # (0,3)
    c_0_3 = driver.find_element(By.CSS_SELECTOR, '#c_0_3').text
    if c_0_3 == '':
        c_0_3 = 0
        list4.append(c_0_3)
    else:
        list4.append(int(c_0_3))
    # (1,3)
    c_1_3 = driver.find_element(By.CSS_SELECTOR, '#c_1_3').text
    if c_1_3 == '':
        c_1_3 = 0
        list4.append(c_1_3)
    else:
        list4.append(int(c_1_3))
    # (2,3)
    c_2_3 = driver.find_element(By.CSS_SELECTOR, '#c_2_3').text
    if c_2_3 == '':
        c_2_3 = 0
        list4.append(c_2_3)
    else:
        list4.append(int(c_2_3))
    # (3,3)
    c_3_3 = driver.find_element(By.CSS_SELECTOR, '#c_3_3').text
    if c_3_3 == '':
        c_3_3 = 0
        list4.append(c_3_3)
    else:
        list4.append(int(c_3_3))
        
    # (4,3)
    c_4_3 = driver.find_element(By.CSS_SELECTOR, '#c_4_3').text
    if c_4_3 == '':
        c_4_3 = 0
        list4.append(c_4_3)
    else:
        list4.append(int(c_4_3))
    # (5,3)
    c_5_3 = driver.find_element(By.CSS_SELECTOR, '#c_5_3').text
    if c_5_3 == '':
        c_5_3 = 0
        list4.append(c_5_3)
    else:
        list4.append(int(c_5_3))
    # (6,3)
    c_6_3 = driver.find_element(By.CSS_SELECTOR, '#c_6_3').text
    if c_6_3 == '':
        c_6_3 = 0
        list4.append(c_6_3)
    else:
        list4.append(int(c_6_3))
    # (7,3)
    c_7_3 = driver.find_element(By.CSS_SELECTOR, '#c_7_3').text
    if c_7_3 == '':
        c_7_3 = 0
        list4.append(c_7_3)
    else:
        list4.append(int(c_7_3))
    # (8,3)
    c_8_3 = driver.find_element(By.CSS_SELECTOR, '#c_8_3').text
    if c_8_3 == '':
        c_8_3 = 0
        list4.append(c_8_3)
    else:
        list4.append(int(c_8_3))
    list5 = []
    # (0,4)
    c_0_4 = driver.find_element(By.CSS_SELECTOR, '#c_0_4').text
    if c_0_4 == '':
        c_0_4 = 0
        list5.append(c_0_4)
    else:
        list5.append(int(c_0_4))
    # (1,4)
    c_1_4 = driver.find_element(By.CSS_SELECTOR, '#c_1_4').text
    if c_1_4 == '':
        c_1_4 = 0
        list5.append(c_1_4)
    else:
        list5.append(int(c_1_4))
    # (2,4)
    c_2_4 = driver.find_element(By.CSS_SELECTOR, '#c_2_4').text
    if c_2_4 == '':
        c_2_4 = 0
        list5.append(c_2_4)
    else:
        list5.append(int(c_2_4))
    # (3,4)
    c_3_4 = driver.find_element(By.CSS_SELECTOR, '#c_3_4').text
    if c_3_4 == '':
        c_3_4 = 0
        list5.append(c_3_4)
    else:
        list5.append(int(c_3_4))
    # (4,4)
    c_4_4 = driver.find_element(By.CSS_SELECTOR, '#c_4_4').text
    if c_4_4 == '':
        c_4_4 = 0
        list5.append(c_4_4)
    else:
        list5.append(int(c_4_4))
    # (5,4)
    c_5_4 = driver.find_element(By.CSS_SELECTOR, '#c_5_4').text
    if c_5_4 == '':
        c_5_4 = 0
        list5.append(c_5_4)
    else:
        list5.append(int(c_5_4))
    # (6,4)
    c_6_4 = driver.find_element(By.CSS_SELECTOR, '#c_6_4').text
    if c_6_4 == '':
        c_6_4 = 0
        list5.append(c_6_4)
    else:
        list5.append(int(c_6_4))
    # (7,4)
    c_7_4 = driver.find_element(By.CSS_SELECTOR, '#c_7_4').text
    if c_7_4 == '':
        c_7_4 = 0
        list5.append(c_7_4)
    else:
        list5.append(int(c_7_4))
    # (8,4)
    c_8_4 = driver.find_element(By.CSS_SELECTOR, '#c_8_4').text
    if c_8_4 == '':
        c_8_4 = 0
        list5.append(c_8_4)
    else:
        list5.append(int(c_8_4))
    list6 = []
    # (0,5)
    c_0_5 = driver.find_element(By.CSS_SELECTOR, '#c_0_5').text
    if c_0_5 == '':
        c_0_5 = 0
        list6.append(c_0_5)
    else:
        list6.append(int(c_0_5))
    # (1,5)
    c_1_5 = driver.find_element(By.CSS_SELECTOR, '#c_1_5').text
    if c_1_5 == '':
        c_1_5 = 0
        list6.append(c_1_5)
    else:
        list6.append(int(c_1_5))
    # (2,5)
    c_2_5 = driver.find_element(By.CSS_SELECTOR, '#c_2_5').text
    if c_2_5 == '':
        c_2_5 = 0
        list6.append(c_2_5)
    else:
        list6.append(int(c_2_5))
    # (3,5)
    c_3_5 = driver.find_element(By.CSS_SELECTOR, '#c_3_5').text
    if c_3_5 == '':
        c_3_5 = 0
        list6.append(c_3_5)
    else:
        list6.append(int(c_3_5))
        
    # (4,5)
    c_4_5 = driver.find_element(By.CSS_SELECTOR, '#c_4_5').text
    if c_4_5 == '':
        c_4_5 = 0
        list6.append(c_4_5)
    else:
        list6.append(int(c_4_5))
    # (5,5)
    c_5_5 = driver.find_element(By.CSS_SELECTOR, '#c_5_5').text
    if c_5_5 == '':
        c_5_5 = 0
        list6.append(c_5_5)
    else:
        list6.append(int(c_5_5))
    # (6,5)
    c_6_5 = driver.find_element(By.CSS_SELECTOR, '#c_6_5').text
    if c_6_5 == '':
        c_6_5 = 0
        list6.append(c_6_5)
    else:
        list6.append(int(c_6_5))
    # (7,5)
    c_7_5 = driver.find_element(By.CSS_SELECTOR, '#c_7_5').text
    if c_7_5 == '':
        c_7_5 = 0
        list6.append(c_7_5)
    else:
        list6.append(int(c_7_5))
    # (8,5)
    c_8_5 = driver.find_element(By.CSS_SELECTOR, '#c_8_5').text
    if c_8_5 == '':
        c_8_5 = 0
        list6.append(c_8_5)
    else:
        list6.append(int(c_8_5))
    list7 = []
    # (0,6)
    c_0_6 = driver.find_element(By.CSS_SELECTOR, '#c_0_6').text
    if c_0_6 == '':
        c_0_6 = 0
        list7.append(c_0_6)
    else:
        list7.append(int(c_0_6))
    # (1,6)
    c_1_6 = driver.find_element(By.CSS_SELECTOR, '#c_1_6').text
    if c_1_6 == '':
        c_1_6 = 0
        list7.append(c_1_6)
    else:
        list7.append(int(c_1_6))
    # (2,6)
    c_2_6 = driver.find_element(By.CSS_SELECTOR, '#c_2_6').text
    if c_2_6 == '':
        c_2_6 = 0
        list7.append(c_2_6)
    else:
        list7.append(int(c_2_6))
    # (3,6)
    c_3_6 = driver.find_element(By.CSS_SELECTOR, '#c_3_6').text
    if c_3_6 == '':
        c_3_6 = 0
        list7.append(c_3_6)
    else:
        list7.append(int(c_3_6))
    # (4,6)
    c_4_6 = driver.find_element(By.CSS_SELECTOR, '#c_4_6').text
    if c_4_6 == '':
        c_4_6 = 0
        list7.append(c_4_6)
    else:
        list7.append(int(c_4_6))
    # (5,6)
    c_5_6 = driver.find_element(By.CSS_SELECTOR, '#c_5_6').text
    if c_5_6 == '':
        c_5_6 = 0
        list7.append(c_5_6)
    else:
        list7.append(int(c_5_6))
    # (6,6)
    c_6_6 = driver.find_element(By.CSS_SELECTOR, '#c_6_6').text
    if c_6_6 == '':
        c_6_6 = 0
        list7.append(c_6_6)
    else:
        list7.append(int(c_6_6))
    # (7,6)
    c_7_6 = driver.find_element(By.CSS_SELECTOR, '#c_7_6').text
    if c_7_6 == '':
        c_7_6 = 0
        list7.append(c_7_6)
    else:
        list7.append(int(c_7_6))
    # (8,6)
    c_8_6 = driver.find_element(By.CSS_SELECTOR, '#c_8_6').text
    if c_8_6 == '':
        c_8_6 = 0
        list7.append(c_8_6)
    else:
        list7.append(int(c_8_6))
    list8 = []
    # (0,7)
    c_0_7 = driver.find_element(By.CSS_SELECTOR, '#c_0_7').text
    if c_0_7 == '':
        c_0_7 = 0
        list8.append(c_0_7)
    else:
        list8.append(int(c_0_7))
    # (1,7)
    c_1_7 = driver.find_element(By.CSS_SELECTOR, '#c_1_7').text
    if c_1_7 == '':
        c_1_7 = 0
        list8.append(c_1_7)
    else:
        list8.append(int(c_1_7))
    # (2,7)
    c_2_7 = driver.find_element(By.CSS_SELECTOR, '#c_2_7').text
    if c_2_7 == '':
        c_2_7 = 0
        list8.append(c_2_7)
    else:
        list8.append(int(c_2_7))
    # (3,7)
    c_3_7 = driver.find_element(By.CSS_SELECTOR, '#c_3_7').text
    if c_3_7 == '':
        c_3_7 = 0
        list8.append(c_3_7)
    else:
        list8.append(int(c_3_7))
    # (4,7)
    c_4_7 = driver.find_element(By.CSS_SELECTOR, '#c_4_7').text
    if c_4_7 == '':
        c_4_7 = 0
        list8.append(c_4_7)
    else:
        list8.append(int(c_4_7))
    # (5,7)
    c_5_7 = driver.find_element(By.CSS_SELECTOR, '#c_5_7').text
    if c_5_7 == '':
        c_5_7 = 0
        list8.append(c_5_7)
    else:
        list8.append(int(c_5_7))
    # (6,7)
    c_6_7 = driver.find_element(By.CSS_SELECTOR, '#c_6_7').text
    if c_6_7 == '':
        c_6_7 = 0
        list8.append(c_6_7)
    else:
        list8.append(int(c_6_7))
    # (7,7)
    c_7_7 = driver.find_element(By.CSS_SELECTOR, '#c_7_7').text
    if c_7_7 == '':
        c_7_7 = 0
        list8.append(c_7_7)
    else:
        list8.append(int(c_7_7))
    # (8,7)
    c_8_7 = driver.find_element(By.CSS_SELECTOR, '#c_8_7').text
    if c_8_7 == '':
        c_8_7 = 0
        list8.append(c_8_7)
    else:
        list8.append(int(c_8_7))
    list9 = []
    # (0,8)
    c_0_8 = driver.find_element(By.CSS_SELECTOR, '#c_0_8').text
    if c_0_8 == '':
        c_0_8 = 0
        list9.append(c_0_8)
    else:
        list9.append(int(c_0_8))
    # (1,8)
    c_1_8 = driver.find_element(By.CSS_SELECTOR, '#c_1_8').text
    if c_1_8 == '':
        c_1_8 = 0
        list9.append(c_1_8)
    else:
        list9.append(int(c_1_8))
    # (2,8)
    c_2_8 = driver.find_element(By.CSS_SELECTOR, '#c_2_8').text
    if c_2_8 == '':
        c_2_8 = 0
        list9.append(c_2_8)
    else:
        list9.append(int(c_2_8))
    # (3,8)
    c_3_8 = driver.find_element(By.CSS_SELECTOR, '#c_3_8').text
    if c_3_8 == '':
        c_3_8 = 0
        list9.append(c_3_8)
    else:
        list9.append(int(c_3_8))
    # (4,8)
    c_4_8 = driver.find_element(By.CSS_SELECTOR, '#c_4_8').text
    if c_4_8 == '':
        c_4_8 = 0
        list9.append(c_4_8)
    else:
        list9.append(int(c_4_8))
    # (5,8)
    c_5_8 = driver.find_element(By.CSS_SELECTOR, '#c_5_8').text
    if c_5_8 == '':
        c_5_8 = 0
        list9.append(c_5_8)
    else:
        list9.append(int(c_5_8))
    # (6,8)
    c_6_8 = driver.find_element(By.CSS_SELECTOR, '#c_6_8').text
    if c_6_8 == '':
        c_6_8 = 0
        list9.append(c_6_8)
    else:
        list9.append(int(c_6_8))
    # (7,8)
    c_7_8 = driver.find_element(By.CSS_SELECTOR, '#c_7_8').text
    if c_7_8 == '':
        c_7_8 = 0
        list9.append(c_7_8)
    else:
        list9.append(int(c_7_8))
    # (8,8)
    c_8_8 = driver.find_element(By.CSS_SELECTOR, '#c_8_8').text
    if c_8_8 == '':
        c_8_8 = 0
        list9.append(c_8_8)
    else:
        list9.append(int(c_8_8))
    list = []
    list.append(list1)
    list.append(list2)
    list.append(list3)
    list.append(list4)
    list.append(list5)
    list.append(list6)
    list.append(list7)
    list.append(list8)
    list.append(list9)

我们看看这里会是什么效果:

 

解数独

这里,我们就不说解数独的思路了,我们直接展示代码,代码中的m就是我们上面代码中获取的的列表值,我们只要把m传进去就可以。

    sys.setrecursionlimit(10000)
    def get_next    (m, x, y):
        for next_y in range(y + 1, 9): 
            if m[x][next_y] == 0:
                return x, next_y
        for next_x in range(x + 1, 9): 
            for next_y in range(0, 9):
                if m[next_x][next_y] == 0:
                    return next_x, next_y
        return -1, -1
    def value (m, x, y)::
        i, j = x // 3, y // 3
        grid = [m[i * 3 + r][j * 3 + c] for r in range(3) for c in range(3)]
        v = set([x for x in range(1, 10)]) - set(grid) - set(m[x]) - \
            set(list(zip(*m))[y])
        return list(v)
    def start_pos(m):
        for x in range(9):
            for y in range(9):
                if m[x][y] == 0:
                    return x, y
        return False, False
    def try_sudoku (m, x, y)::
        for v in value(m, x, y):
            m[x][y] = v
            next_x, next_y = get_next(m, x, y)
            if next_y == -1:  
                return True
            else:
                end = try_sudoku(m, next_x, next_y) 
                if end:
                    return True
                m[x][y] = 0  
    def sudoku(m):
        x, y = start_pos(m)
        try_sudoku(m, x, y)
        print(m)
        tianxie_shudu(m)
    sudoku(m)

我们这样就把数独解出来了,接下来,就是来模拟填写数独了。

模拟填写数独

这个思路很简单,就是判断数独中空值的地方,就填写对应的数字。

    c_0_0 = driver.find_element(By.CSS_SELECTOR, '#c_0_0').text
    if c_0_0 == '':
        print(list_m[0])
        driver.find_element(By.CSS_SELECTOR, '#c_0_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[0]}').click()
        time.sleep(1)
    else:
        pass

这段代码是通过find_element方法找到一个元素,并获取该元素的文本。然后,它检查这个文本是否为空。如果为空,它会执行以下操作:

  1. 打印一个名为list_m[0]的值。
  2. 找到一个元素,使用CSS选择器'#c_0_0'定位,并模拟点击事件。
  3. 等待1秒钟,以等待页面加载或元素切换等操作完成。
  4. 找到一个元素,使用CSS选择器'#ot{list_m[0]}'定位,并模拟点击事件。
  5. 等待1秒钟,以等待页面加载或元素切换等操作完成。

如果文本不为空,则不执行任何操作,使用pass表示跳过。

其他的都是类似的,我就不一一解释了。下面直接看代码:

    list_m = []
    for x in m:
        for y in x:
            list_m.append(y)
    print(list_m)
    driver.find_element(By.CSS_SELECTOR, '#rv').click()
    c_0_0 = driver.find_element(By.CSS_SELECTOR, '#c_0_0').text
    if c_0_0 == '':
        print(list_m[0])
        driver.find_element(By.CSS_SELECTOR, '#c_0_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[0]}').click()
        time.sleep(1)
    else:
        pass
    c_1_0 = driver.find_element(By.CSS_SELECTOR, '#c_1_0').text
    if c_1_0 == '':
        print(list_m[1])
        driver.find_element(By.CSS_SELECTOR, '#c_1_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[1]}').click()
        time.sleep(1)
    else:
        pass
    c_2_0 = driver.find_element(By.CSS_SELECTOR, '#c_2_0').text
    if c_2_0 == '':
        print(list_m[2])
        driver.find_element(By.CSS_SELECTOR, '#c_2_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[2]}').click()
        time.sleep(1)
    else:
        pass
    c_3_0 = driver.find_element(By.CSS_SELECTOR, '#c_3_0').text
    if c_3_0 == '':
        print(list_m[3])
        driver.find_element(By.CSS_SELECTOR, '#c_3_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[3]}').click()
        time.sleep(1)
    else:
        pass
    c_4_0 = driver.find_element(By.CSS_SELECTOR, '#c_4_0').text
    if c_4_0 == '':
        print(list_m[4])
        driver.find_element(By.CSS_SELECTOR, '#c_4_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[4]}').click()
        time.sleep(1)
    else:
        pass
    c_5_0 = driver.find_element(By.CSS_SELECTOR, '#c_5_0').text
    if c_5_0 == '':
        print(list_m[5])
        driver.find_element(By.CSS_SELECTOR, '#c_5_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[5]}').click()
        time.sleep(1)
    else:
        pass
    c_6_0 = driver.find_element(By.CSS_SELECTOR, '#c_6_0').text
    if c_6_0 == '':
        print(list_m[6])
        driver.find_element(By.CSS_SELECTOR, '#c_6_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[6]}').click()
        time.sleep(1)
    else:
        pass
    c_7_0 = driver.find_element(By.CSS_SELECTOR, '#c_7_0').text
    if c_7_0 == '':
        print(list_m[7])
        driver.find_element(By.CSS_SELECTOR, '#c_7_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[7]}').click()
        time.sleep(1)
    else:
        pass
    c_8_0 = driver.find_element(By.CSS_SELECTOR, '#c_8_0').text
    if c_8_0 == '':
        print(list_m[8])
        driver.find_element(By.CSS_SELECTOR, '#c_8_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[8]}').click()
        time.sleep(1)
    else:
        pass
    c_0_1 = driver.find_element(By.CSS_SELECTOR, '#c_0_1').text
    if c_0_1 == '':
        print(list_m[9])
        driver.find_element(By.CSS_SELECTOR, '#c_0_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[9]}').click()
        time.sleep(1)
    else:
        pass
    c_1_1 = driver.find_element(By.CSS_SELECTOR, '#c_1_1').text
    if c_1_1 == '':
        print(list_m[10])
        driver.find_element(By.CSS_SELECTOR, '#c_1_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[10]}').click()
        time.sleep(1)
    else:
        pass
    c_2_1 = driver.find_element(By.CSS_SELECTOR, '#c_2_1').text
    if c_2_1 == '':
        print(list_m[11])
        driver.find_element(By.CSS_SELECTOR, '#c_2_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[11]}').click()
        time.sleep(1)
    else:
        pass
    c_3_1 = driver.find_element(By.CSS_SELECTOR, '#c_3_1').text
    if c_3_1 == '':
        print(list_m[12])
        driver.find_element(By.CSS_SELECTOR, '#c_3_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[12]}').click()
        time.sleep(1)
    else:
        pass
    c_4_1 = driver.find_element(By.CSS_SELECTOR, '#c_4_1').text
    if c_4_1 == '':
        print(list_m[13])
        driver.find_element(By.CSS_SELECTOR, '#c_4_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[13]}').click()
        time.sleep(1)
    else:
        pass
    c_5_1 = driver.find_element(By.CSS_SELECTOR, '#c_5_1').text
    if c_5_1 == '':
        print(list_m[14])
        driver.find_element(By.CSS_SELECTOR, '#c_5_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[14]}').click()
        time.sleep(1)
    else:
        pass
    c_6_1 = driver.find_element(By.CSS_SELECTOR, '#c_6_1').text
    if c_6_1 == '':
        print(list_m[15])
        driver.find_element(By.CSS_SELECTOR, '#c_6_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[15]}').click()
        time.sleep(1)
    else:
        pass
    c_7_1 = driver.find_element(By.CSS_SELECTOR, '#c_7_1').text
    if c_7_1 == '':
        print(list_m[16])
        driver.find_element(By.CSS_SELECTOR, '#c_7_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[16]}').click()
        time.sleep(1)
    else:
        pass
    c_8_1 = driver.find_element(By.CSS_SELECTOR, '#c_8_1').text
    if c_8_1 == '':
        print(list_m[17])
        driver.find_element(By.CSS_SELECTOR, '#c_8_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[17]}').click()
        time.sleep(1)
    else:
        pass
    c_0_2 = driver.find_element(By.CSS_SELECTOR, '#c_0_2').text
    if c_0_2 == '':
        print(list_m[18])
        driver.find_element(By.CSS_SELECTOR, '#c_0_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[18]}').click()
        time.sleep(1)
    else:
        pass
    c_1_2 = driver.find_element(By.CSS_SELECTOR, '#c_1_2').text
    if c_1_2 == '':
        print(list_m[19])
        driver.find_element(By.CSS_SELECTOR, '#c_1_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[19]}').click()
        time.sleep(1)
    else:
        pass
    c_2_2 = driver.find_element(By.CSS_SELECTOR, '#c_2_2').text
    if c_2_2 == '':
        print(list_m[20])
        driver.find_element(By.CSS_SELECTOR, '#c_2_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[20]}').click()
        time.sleep(1)
    else:
        pass
    c_3_2 = driver.find_element(By.CSS_SELECTOR, '#c_3_2').text
    if c_3_2 == '':
        print(list_m[21])
        driver.find_element(By.CSS_SELECTOR, '#c_3_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[21]}').click()
        time.sleep(1)
    else:
        pass
    c_4_2 = driver.find_element(By.CSS_SELECTOR, '#c_4_2').text
    if c_4_2 == '':
        print(list_m[22])
        driver.find_element(By.CSS_SELECTOR, '#c_4_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[22]}').click()
        time.sleep(1)
    else:
        pass
    c_5_2 = driver.find_element(By.CSS_SELECTOR, '#c_5_2').text
    if c_5_2 == '':
        print(list_m[23])
        driver.find_element(By.CSS_SELECTOR, '#c_5_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[23]}').click()
        time.sleep(1)
    else:
        pass
    c_6_2 = driver.find_element(By.CSS_SELECTOR, '#c_6_2').text
    if c_6_2 == '':
        print(list_m[24])
        driver.find_element(By.CSS_SELECTOR, '#c_6_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[24]}').click()
        time.sleep(1)
    else:
        pass
    c_7_2 = driver.find_element(By.CSS_SELECTOR, '#c_7_2').text
    if c_7_2 == '':
        print(list_m[25])
        driver.find_element(By.CSS_SELECTOR, '#c_7_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[25]}').click()
        time.sleep(1)
    else:
        pass
    c_8_2 = driver.find_element(By.CSS_SELECTOR, '#c_8_2').text
    if c_8_2 == '':
        print(list_m[26])
        driver.find_element(By.CSS_SELECTOR, '#c_8_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[26]}').click()
        time.sleep(1)
    else:
        pass
    c_0_3 = driver.find_element(By.CSS_SELECTOR, '#c_0_3').text
    if c_0_3 == '':
        print(list_m[27])
        driver.find_element(By.CSS_SELECTOR, '#c_0_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[27]}').click()
        time.sleep(1)
    else:
        pass
    c_1_3 = driver.find_element(By.CSS_SELECTOR, '#c_1_3').text
    if c_1_3 == '':
        print(list_m[28])
        driver.find_element(By.CSS_SELECTOR, '#c_1_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[28]}').click()
        time.sleep(1)
    else:
        pass
    c_2_3 = driver.find_element(By.CSS_SELECTOR, '#c_2_3').text
    if c_2_3 == '':
        print(list_m[29])
        driver.find_element(By.CSS_SELECTOR, '#c_2_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[29]}').click()
        time.sleep(1)
    else:
        pass
    c_3_3 = driver.find_element(By.CSS_SELECTOR, '#c_3_3').text
    if c_3_3 == '':
        print(list_m[30])
        driver.find_element(By.CSS_SELECTOR, '#c_3_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[30]}').click()
        time.sleep(1)
    else:
        pass
    c_4_3 = driver.find_element(By.CSS_SELECTOR, '#c_4_3').text
    if c_4_3 == '':
        print(list_m[31])
        driver.find_element(By.CSS_SELECTOR, '#c_4_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[31]}').click()
        time.sleep(1)
    else:
        pass
    c_5_3 = driver.find_element(By.CSS_SELECTOR, '#c_5_3').text
    if c_5_3 == '':
        print(list_m[32])
        driver.find_element(By.CSS_SELECTOR, '#c_5_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[32]}').click()
        time.sleep(1)
    else:
        pass
    c_6_3 = driver.find_element(By.CSS_SELECTOR, '#c_6_3').text
    if c_6_3 == '':
        print(list_m[33])
        driver.find_element(By.CSS_SELECTOR, '#c_6_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[33]}').click()
        time.sleep(1)
    else:
        pass
    c_7_3 = driver.find_element(By.CSS_SELECTOR, '#c_7_3').text
    if c_7_3 == '':
        print(list_m[34])
        driver.find_element(By.CSS_SELECTOR, '#c_7_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[34]}').click()
        time.sleep(1)
    else:
        pass
    c_8_3 = driver.find_element(By.CSS_SELECTOR, '#c_8_3').text
    if c_8_3 == '':
        print(list_m[35])
        driver.find_element(By.CSS_SELECTOR, '#c_8_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[35]}').click()
        time.sleep(1)
    else:
        pass
    c_0_4 = driver.find_element(By.CSS_SELECTOR, '#c_0_4').text
    if c_0_4 == '':
        print(list_m[36])
        driver.find_element(By.CSS_SELECTOR, '#c_0_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[36]}').click()
        time.sleep(1)
    else:
        pass
    c_1_4 = driver.find_element(By.CSS_SELECTOR, '#c_1_4').text
    if c_1_4 == '':
        print(list_m[37])
        driver.find_element(By.CSS_SELECTOR, '#c_1_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[37]}').click()
        time.sleep(1)
    else:
        pass
    c_2_4 = driver.find_element(By.CSS_SELECTOR, '#c_2_4').text
    if c_2_4 == '':
        print(list_m[38])
        driver.find_element(By.CSS_SELECTOR, '#c_2_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[38]}').click()
        time.sleep(1)
    else:
        pass
    c_3_4 = driver.find_element(By.CSS_SELECTOR, '#c_3_4').text
    if c_3_4 == '':
        print(list_m[39])
        driver.find_element(By.CSS_SELECTOR, '#c_3_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[39]}').click()
        time.sleep(1)
    else:
        pass
    c_4_4 = driver.find_element(By.CSS_SELECTOR, '#c_4_4').text
    if c_4_4 == '':
        print(list_m[40])
        driver.find_element(By.CSS_SELECTOR, '#c_4_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[40]}').click()
        time.sleep(1)
    else:
        pass
    c_5_4 = driver.find_element(By.CSS_SELECTOR, '#c_5_4').text
    if c_5_4 == '':
        print(list_m[41])
        driver.find_element(By.CSS_SELECTOR, '#c_5_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[41]}').click()
        time.sleep(1)
    else:
        pass
    c_6_4 = driver.find_element(By.CSS_SELECTOR, '#c_6_4').text
    if c_6_4 == '':
        print(list_m[42])
        driver.find_element(By.CSS_SELECTOR, '#c_6_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[42]}').click()
        time.sleep(1)
    else:
        pass
    c_7_4 = driver.find_element(By.CSS_SELECTOR, '#c_7_4').text
    if c_7_4 == '':
        print(list_m[43])
        driver.find_element(By.CSS_SELECTOR, '#c_7_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[43]}').click()
        time.sleep(1)
    else:
        pass
    c_8_4 = driver.find_element(By.CSS_SELECTOR, '#c_8_4').text
    if c_8_4 == '':
        print(list_m[44])
        driver.find_element(By.CSS_SELECTOR, '#c_8_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[44]}').click()
        time.sleep(1)
    else:
        pass
    c_0_5 = driver.find_element(By.CSS_SELECTOR, '#c_0_5').text
    if c_0_5 == '':
        print(list_m[45])
        driver.find_element(By.CSS_SELECTOR, '#c_0_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[45]}').click()
        time.sleep(1)
    else:
        pass
    c_1_5 = driver.find_element(By.CSS_SELECTOR, '#c_1_5').text
    if c_1_5 == '':
        print(list_m[46])
        driver.find_element(By.CSS_SELECTOR, '#c_1_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[46]}').click()
        time.sleep(1)
    else:
        pass
    c_2_5 = driver.find_element(By.CSS_SELECTOR, '#c_2_5').text
    if c_2_5 == '':
        print(list_m[47])
        driver.find_element(By.CSS_SELECTOR, '#c_2_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[47]}').click()
        time.sleep(1)
    else:
        pass
    c_3_5 = driver.find_element(By.CSS_SELECTOR, '#c_3_5').text
    if c_3_5 == '':
        print(list_m[48])
        driver.find_element(By.CSS_SELECTOR, '#c_3_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[48]}').click()
        time.sleep(1)
    else:
        pass
    c_4_5 = driver.find_element(By.CSS_SELECTOR, '#c_4_5').text
    if c_4_5 == '':
        print(list_m[49])
        driver.find_element(By.CSS_SELECTOR, '#c_4_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[49]}').click()
        time.sleep(1)
    else:
        pass
    c_5_5 = driver.find_element(By.CSS_SELECTOR, '#c_5_5').text
    if c_5_5 == '':
        print(list_m[50])
        driver.find_element(By.CSS_SELECTOR, '#c_5_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[50]}').click()
        time.sleep(1)
    else:
        pass
    c_6_5 = driver.find_element(By.CSS_SELECTOR, '#c_6_5').text
    if c_6_5 == '':
        print(list_m[51])
        driver.find_element(By.CSS_SELECTOR, '#c_6_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[51]}').click()
        time.sleep(1)
    else:
        pass
    c_7_5 = driver.find_element(By.CSS_SELECTOR, '#c_7_5').text
    if c_7_5 == '':
        print(list_m[52])
        driver.find_element(By.CSS_SELECTOR, '#c_7_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[52]}').click()
        time.sleep(1)
    else:
        pass
    c_8_5 = driver.find_element(By.CSS_SELECTOR, '#c_8_5').text
    if c_8_5 == '':
        print(list_m[53])
        driver.find_element(By.CSS_SELECTOR, '#c_8_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[53]}').click()
        time.sleep(1)
    else:
        pass
    c_0_6 = driver.find_element(By.CSS_SELECTOR, '#c_0_6').text
    if c_0_6 == '':
        print(list_m[54])
        driver.find_element(By.CSS_SELECTOR, '#c_0_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[54]}').click()
        time.sleep(1)
    else:
        pass
    c_1_6 = driver.find_element(By.CSS_SELECTOR, '#c_1_6').text
    if c_1_6 == '':
        print(list_m[55])
        driver.find_element(By.CSS_SELECTOR, '#c_1_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[55]}').click()
        time.sleep(1)
    else:
        pass
    c_2_6 = driver.find_element(By.CSS_SELECTOR, '#c_2_6').text
    if c_2_6 == '':
        print(list_m[56])
        driver.find_element(By.CSS_SELECTOR, '#c_2_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[56]}').click()
        time.sleep(1)
    else:
        pass
    c_3_6 = driver.find_element(By.CSS_SELECTOR, '#c_3_6').text
    if c_3_6 == '':
        print(list_m[57])
        driver.find_element(By.CSS_SELECTOR, '#c_3_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[57]}').click()
        time.sleep(1)
    else:
        pass
    c_4_6 = driver.find_element(By.CSS_SELECTOR, '#c_4_6').text
    if c_4_6 == '':
        print(list_m[58])
        driver.find_element(By.CSS_SELECTOR, '#c_4_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[58]}').click()
        time.sleep(1)
    else:
        pass
    c_5_6 = driver.find_element(By.CSS_SELECTOR, '#c_5_6').text
    if c_5_6 == '':
        print(list_m[59])
        driver.find_element(By.CSS_SELECTOR, '#c_5_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[59]}').click()
        time.sleep(1)
    else:
        pass
    c_6_6 = driver.find_element(By.CSS_SELECTOR, '#c_6_6').text
    if c_6_6 == '':
        print(list_m[60])
        driver.find_element(By.CSS_SELECTOR, '#c_6_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[60]}').click()
        time.sleep(1)
    else:
        pass
    c_7_6 = driver.find_element(By.CSS_SELECTOR, '#c_7_6').text
    if c_7_6 == '':
        print(list_m[61])
        driver.find_element(By.CSS_SELECTOR, '#c_7_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[61]}').click()
        time.sleep(1)
    else:
        pass
    c_8_6 = driver.find_element(By.CSS_SELECTOR, '#c_8_6').text
    if c_8_6 == '':
        print(list_m[62])
        driver.find_element(By.CSS_SELECTOR, '#c_8_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[62]}').click()
        time.sleep(1)
    else:
        pass
    c_0_7 = driver.find_element(By.CSS_SELECTOR, '#c_0_7').text
    if c_0_7 == '':
        print(list_m[63])
        driver.find_element(By.CSS_SELECTOR, '#c_0_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[63]}').click()
        time.sleep(1)
    else:
        pass
    c_1_7 = driver.find_element(By.CSS_SELECTOR, '#c_1_7').text
    if c_1_7 == '':
        print(list_m[64])
        driver.find_element(By.CSS_SELECTOR, '#c_1_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[64]}').click()
        time.sleep(1)
    else:
        pass
    c_2_7 = driver.find_element(By.CSS_SELECTOR, '#c_2_7').text
    if c_2_7 == '':
        print(list_m[65])
        driver.find_element(By.CSS_SELECTOR, '#c_2_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[65]}').click()
        time.sleep(1)
    else:
        pass
    c_3_7 = driver.find_element(By.CSS_SELECTOR, '#c_3_7').text
    if c_3_7 == '':
        print(list_m[66])
        driver.find_element(By.CSS_SELECTOR, '#c_3_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[66]}').click()
        time.sleep(1)
    else:
        pass
    c_4_7 = driver.find_element(By.CSS_SELECTOR, '#c_4_7').text
    if c_4_7 == '':
        print(list_m[67])
        driver.find_element(By.CSS_SELECTOR, '#c_4_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[67]}').click()
        time.sleep(1)
    else:
        pass
    c_5_7 = driver.find_element(By.CSS_SELECTOR, '#c_5_7').text
    if c_5_7 == '':
        print(list_m[68])
        driver.find_element(By.CSS_SELECTOR, '#c_5_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[68]}').click()
        time.sleep(1)
    else:
        pass
    c_6_7 = driver.find_element(By.CSS_SELECTOR, '#c_6_7').text
    if c_6_7 == '':
        print(list_m[69])
        driver.find_element(By.CSS_SELECTOR, '#c_6_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[69]}').click()
        time.sleep(1)
    else:
        pass
    c_7_7 = driver.find_element(By.CSS_SELECTOR, '#c_7_7').text
    if c_7_7 == '':
        print(list_m[70])
        driver.find_element(By.CSS_SELECTOR, '#c_7_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[70]}').click()
        time.sleep(1)
    else:
        pass
    c_8_7 = driver.find_element(By.CSS_SELECTOR, '#c_8_7').text
    if c_8_7 == '':
        print(list_m[71])
        driver.find_element(By.CSS_SELECTOR, '#c_8_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[71]}').click()
        time.sleep(1)
    else:
        pass
    c_0_8 = driver.find_element(By.CSS_SELECTOR, '#c_0_8').text
    if c_0_8 == '':
        print(list_m[72])
        driver.find_element(By.CSS_SELECTOR, '#c_0_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[72]}').click()
        time.sleep(1)
    else:
        pass
    c_1_8 = driver.find_element(By.CSS_SELECTOR, '#c_1_8').text
    if c_1_8 == '':
        print(list_m[73])
        driver.find_element(By.CSS_SELECTOR, '#c_1_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[73]}').click()
        time.sleep(1)
    else:
        pass
    c_2_8 = driver.find_element(By.CSS_SELECTOR, '#c_2_8').text
    if c_2_8 == '':
        print(list_m[74])
        driver.find_element(By.CSS_SELECTOR, '#c_2_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[74]}').click()
        time.sleep(1)
    else:
        pass
    c_3_8 = driver.find_element(By.CSS_SELECTOR, '#c_3_8').text
    if c_3_8 == '':
        print(list_m[75])
        driver.find_element(By.CSS_SELECTOR, '#c_3_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[75]}').click()
        time.sleep(1)
    else:
        pass
    c_4_8 = driver.find_element(By.CSS_SELECTOR, '#c_4_8').text
    if c_4_8 == '':
        print(list_m[76])
        driver.find_element(By.CSS_SELECTOR, '#c_4_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[76]}').click()
        time.sleep(1)
    else:
        pass
    c_5_8 = driver.find_element(By.CSS_SELECTOR, '#c_5_8').text
    if c_5_8 == '':
        print(list_m[77])
        driver.find_element(By.CSS_SELECTOR, '#c_5_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[77]}').click()
        time.sleep(1)
    else:
        pass
    c_6_8 = driver.find_element(By.CSS_SELECTOR, '#c_6_8').text
    if c_6_8 == '':
        print(list_m[78])
        driver.find_element(By.CSS_SELECTOR, '#c_6_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[78]}').click()
        time.sleep(1)
    else:
        pass
    c_7_8 = driver.find_element(By.CSS_SELECTOR, '#c_7_8').text
    if c_7_8 == '':
        print(list_m[79])
        driver.find_element(By.CSS_SELECTOR, '#c_7_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[79]}').click()
        time.sleep(1)
    else:
        pass
    c_8_8 = driver.find_element(By.CSS_SELECTOR, '#c_8_8').text
    if c_8_8 == '':
        print(list_m[80])
        driver.find_element(By.CSS_SELECTOR, '#c_8_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[80]}').click()
        time.sleep(1)
    else:
        pass

我们看看最后的效果吧:

image.png

总结

由于篇幅有限,我这个源码会放在评论区或者放个资源,感兴趣的可以自行下载,这个功能其实特别简单,就是简单的selenium的操作,有什么疑问可以在评论区说说。


相关文章
|
2月前
|
Web App开发 前端开发 JavaScript
探索Python科学计算的边界:利用Selenium进行Web应用性能测试与优化
【10月更文挑战第6天】随着互联网技术的发展,Web应用程序已经成为人们日常生活和工作中不可或缺的一部分。这些应用不仅需要提供丰富的功能,还必须具备良好的性能表现以保证用户体验。性能测试是确保Web应用能够快速响应用户请求并处理大量并发访问的关键步骤之一。本文将探讨如何使用Python结合Selenium来进行Web应用的性能测试,并通过实际代码示例展示如何识别瓶颈及优化应用。
136 5
|
4月前
|
前端开发 JavaScript Java
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
本文介绍了一个结合SpringBoot和Python的实用功能,旨在监控iPhone 15的库存状态并通过邮件提醒用户。系统采用SpringBoot监听苹果官网API,解析JSON数据判断是否有货,并展示最近的库存记录。此外,还能自动触发Selenium+Python脚本实现自动化购买。文中详细介绍了技术栈、接口分析、邮件配置及自动化脚本的设置方法。该项目不仅适用于熟悉后端开发的人员,也适合回顾Layui和Jquery等前端技术。
57 0
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
|
1月前
|
数据采集 Web App开发 前端开发
Python爬虫进阶:Selenium在动态网页抓取中的实战
【10月更文挑战第26天】动态网页抓取是网络爬虫的难点,因为数据通常通过JavaScript异步加载。Selenium通过模拟浏览器行为,可以加载和执行JavaScript,从而获取动态网页的完整内容。本文通过实战案例,介绍如何使用Selenium在Python中抓取动态网页。首先安装Selenium库和浏览器驱动,然后通过示例代码展示如何抓取英国国家美术馆的图片信息。
90 6
|
2月前
|
数据采集 Web App开发 数据可视化
Python爬虫教程:Selenium可视化爬虫的快速入门
Python爬虫教程:Selenium可视化爬虫的快速入门
|
2月前
|
测试技术 数据安全/隐私保护 开发者
自动化测试的奥秘:如何用Selenium和Python提升软件质量
【9月更文挑战第35天】在软件开发的海洋中,自动化测试是那艘能引领我们穿越波涛的帆船。本文将揭开自动化测试的神秘面纱,以Selenium和Python为工具,展示如何构建一个简单而强大的自动化测试框架。我们将从基础出发,逐步深入到高级应用,让读者能够理解并实现自动化测试脚本,从而提升软件的质量与可靠性。
|
3月前
|
Web App开发 测试技术 持续交付
自动化测试的利器:Selenium与Python的完美结合
【9月更文挑战第21天】在软件开发的世界里,测试是确保产品质量的关键步骤。随着敏捷开发和持续集成的流行,自动化测试工具变得尤为重要。本文将介绍如何使用Selenium和Python进行高效的自动化测试,不仅提供代码示例,还深入探讨如何设计测试用例、选择正确的测试框架、以及如何整合到CI/CD流程中。无论你是初学者还是有经验的开发者,这篇文章都将为你提供宝贵的见解和实用的技巧。
55 3
|
3月前
|
机器学习/深度学习 人工智能 TensorFlow
神经网络深度剖析:Python带你潜入AI大脑,揭秘智能背后的秘密神经元
【9月更文挑战第12天】在当今科技飞速发展的时代,人工智能(AI)已深入我们的生活,从智能助手到自动驾驶,从医疗诊断到金融分析,其力量无处不在。这一切的核心是神经网络。本文将带领您搭乘Python的航船,深入AI的大脑,揭秘智能背后的秘密神经元。通过构建神经网络模型,我们可以模拟并学习复杂的数据模式。以下是一个使用Python和TensorFlow搭建的基本神经网络示例,用于解决简单的分类问题。
52 10
|
4月前
|
Web App开发 测试技术 API
自动化测试之美:使用Selenium和Python进行Web应用测试
【8月更文挑战第31天】在软件开发的快节奏世界中,自动化测试如同一束明灯,照亮了质量保证之路。本文将引导你通过Selenium和Python的强大组合,探索如何构建高效的Web应用测试框架。我们不仅会讨论理论,还会深入代码,从一个简单的示例开始,逐步扩展至更复杂的场景。无论你是初学者还是有经验的开发者,这篇文章都将为你提供宝贵的见解和实用的技巧。让我们一同揭开自动化测试的神秘面纱,体验它的魅力所在。
|
4月前
|
Web App开发 XML 测试技术
自动化测试框架设计:以Python和Selenium为例
【8月更文挑战第31天】在软件开发的快节奏中,自动化测试成为确保产品质量的关键步骤。本文将引导读者了解如何结合Python语言和Selenium工具来设计一个高效的自动化测试框架。通过浅显易懂的语言和实际代码示例,我们将探索自动化测试框架的核心组件,并学习如何实现它们。无论你是测试新手还是希望提升自动化技能的开发者,这篇文章都将为你打开一扇通向高效软件测试的大门。
|
3月前
|
敏捷开发 测试技术 持续交付
自动化测试之美:如何用Selenium和Python打造高效测试脚本
【9月更文挑战第13天】在软件开发的海洋中,自动化测试是那抹不可或缺的亮色。它不仅提升了测试效率,还保障了产品质量。本文将带你领略使用Selenium和Python构建自动化测试脚本的魅力所在,从环境的搭建到脚本的编写,再到问题的排查,每一步都是对软件质量把控的深刻理解和实践。让我们开始这段探索之旅,解锁自动化测试的秘密吧!
80 0

热门文章

最新文章