from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
for uuu in range(1,11):
if uuu != 10:
uurl = "0"+ str(uuu)
url = 'http://www.henanjianan.com/tiku/dianhan/'+uurl+'.html'
driver = webdriver.Edge()
driver.get(url)
time.sleep(5)
driver.find_element_by_xpath("/html/body/form/div[5]/input").click()#点击按钮
time.sleep(3)
alertObject = driver.switch_to.alert
print(alertObject.text) # 打印提示信息
time.sleep(1)
alertObject.accept() # 点击确定按钮
for n in range(30):
r=1000
panduanti = "XXLD"+ str(n)
daan = "Tip"+str(r)
#单选题目
aswes = driver.find_element(By.XPATH,'//*[@id="'+str(panduanti)+'"]/div[1]')
print(aswes.text)
with open("D:/test2.txt","a+") as f:
f.write(aswes.text) # 自带文件关闭功能,不需要再写f.close()
#答案
aswes = driver.find_element(By.XPATH,'//*[@id="'+daan+'"]/div[2]')
print(aswes.text)
with open("D:/test2.txt","a+") as f:
f.write(aswes.text) # 自带文件关闭功能,不需要再写f.close()
f.write('\n')
r=r+1
#选项
aswes = driver.find_element(By.XPATH,'//*[@id="'+str(panduanti)+'"]/div[2]')
print(aswes.text)
with open("D:/test2.txt","a+") as f:
f.write(aswes.text) # 自带文件关闭功能,不需要再写f.close()
f.write('\n')
aswes = driver.find_element(By.XPATH,'//*[@id="'+str(panduanti)+'"]/div[3]')
print(aswes.text)
with open("D:/test2.txt","a+") as f:
f.write(aswes.text) # 自带文件关闭功能,不需要再写f.close()
f.write('\n')
aswes = driver.find_element(By.XPATH,'//*[@id="'+str(panduanti)+'"]/div[4]')
print(aswes.text)
with open("D:/test2.txt","a+") as f:
f.write(aswes.text) # 自带文件关闭功能,不需要再写f.close()
f.write('\n')
#判断题目
for n in range(70):
r=1030
panduanti = "PDT"+str(n)
daan = "Tip"+str(r)
aswes = driver.find_element(By.XPATH,'//*[@id="'+panduanti+'"]/div[1]')
print(aswes.text)
with open("D:/test2.txt","a+") as f:
f.write(aswes.text) # 自带文件关闭功能,不需要再写f.close()
aswes = driver.find_element(By.XPATH,'//*[@id="'+daan+'"]/div[2]')
print(aswes.text)
with open("D:/test2.txt","a+") as f:
f.write(aswes.text) # 自带文件关闭功能,不需要再写f.close()
f.write('\n')
r=r+1
driver.close()
会输出到D盘根目录下的test.txt文本中