【发布时间】:2021-04-19 00:04:25
【问题描述】:
我正在尝试从此网站https://brainbooks.pk/newtest-system/free_mcqs.php 生成数据。形式是动态的。在表单中有四个下拉菜单。我可以选择前 3 个(董事会、班级和主题),然后是单元下拉最后一个。带有复选框的那个。我想循环并一一检查。我无法遍历单元元素。这是我的代码:
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://brainbooks.pk/newtest-system/free_mcqs.php')
def get_options(driver, class_name):
drop_down = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, class_name)))
select = Select(drop_down)
return select.options
board_option = driver.find_element_by_xpath('//*[@id="board_id"]/option[2]')
board_option.click()
time.sleep(0.5)
class_options = ['//*[@id="class_id"]/option[2]','//*[@id="class_id"]/option[3]','//*[@id="class_id"]/option[4]','//*[@id="class_id"]/option[5]']
for class_select in class_options:
select_class = driver.find_element_by_xpath(class_select)
print(select_class.text)
select_class.click()
if class_select == '//*[@id="class_id"]/option[2]':
subject_options = get_options(driver, 'subject')
for subj in subject_options[1:]:
print(subj.text)
subj.click()
topic_options = get_options(driver, 'q_unit')
time.sleep(1.5)
for topic in topic_options:
print(topic)
topic.click()
time.sleep(1.5)
break
break
break
break
编辑 1break 是临时的。如果一个循环成功执行它们,我将删除它们并让脚本完全运行。
我尝试使用此代码"//input[@type='checkbox'][@data-chid='" + i+ "']" 循环复选框并收到此错误TypeError: 'WebElement' object is not iterable
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://brainbooks.pk/newtest-system/free_mcqs.php')
def get_options(driver, class_name):
drop_down = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, class_name)))
select = Select(drop_down)
return select.options
board_option = driver.find_element_by_xpath('//*[@id="board_id"]/option[2]')
board_option.click()
time.sleep(0.5)
class_options = ['//*[@id="class_id"]/option[2]','//*[@id="class_id"]/option[3]','//*[@id="class_id"]/option[4]','//*[@id="class_id"]/option[5]']
for class_select in class_options:
select_class = driver.find_element_by_xpath(class_select)
print(select_class.text)
select_class.click()
if class_select == '//*[@id="class_id"]/option[2]':
subject_options = get_options(driver, 'subject')
for subj in subject_options[1:]:
print(subj.text)
subj.click()
topic_options = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'q_unit')))
time.sleep(1.5)
for topic in topic_options:
select_topic = driver.find_element_by_xpath("//input[@type='checkbox'][@data-chid='" + topic + "']").click()
print(select_topic.text)
select_topic.click()
time.sleep(1.5)
break
break
break
break
EDIT 2
1 次迭代之后它工作得非常好,我收到了这个错误 element not interactable
import time
import pyautogui
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://brainbooks.pk/newtest-system/free_mcqs.php')
def get_options(driver, class_name):
drop_down = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, class_name)))
select = Select(drop_down)
return select.options
board_option = driver.find_element_by_xpath('//*[@id="board_id"]/option[2]')
board_option.click()
time.sleep(0.5)
class_options = ['//*[@id="class_id"]/option[2]','//*[@id="class_id"]/option[3]','//*[@id="class_id"]/option[4]','//*[@id="class_id"]/option[5]']
for class_select in class_options:
select_class = driver.find_element_by_xpath(class_select)
select_class.click()
time.sleep(2)
if class_select == '//*[@id="class_id"]/option[2]':
try:
subject_options = get_options(driver, 'subject')
except:
select_class = driver.find_element_by_xpath('//*[@id="class_id"]/option[1]')
select_class.click()
select_class = driver.find_element_by_xpath(class_select)
select_class.click()
time.sleep(2)
finally:
subject_options = get_options(driver, 'subject')
for subj in subject_options[1:]:
print(subj.text)
sub_name = subj.text
subj.click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'mutliSelect')))
units_container = driver.find_element_by_class_name("mutliSelect")
topic_options = units_container.find_elements_by_xpath("//input[@type='checkbox']")
time.sleep(1.5)
for select_topic in topic_options:
topic_name = select_topic.find_element_by_xpath("..")
print(topic_name.text)
select_topic.click()
time.sleep(1.5)
generate_button = (driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[1]/div/button[1]')).click()
time.sleep(3)
view_result_button = (driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div/div[1]/div/button[3]')).click()
#Saving Html Pages
topic_title = topic_name.text
topic_title = topic_title.replace(':', '-')
topic_title = topic_title.replace(' ', ' ')
save_file_name = sub_name+'-'+topic_title
pyautogui.hotkey("ctrlleft", "s") # Saving File
time.sleep(.5)
pyautogui.press('f4') # Opening Address Bar
time.sleep(.5)
pyautogui.hotkey("ctrlleft", "a") # Selecting Previous Address
time.sleep(.5)
pyautogui.press('delete') # Removing Previous Address
time.sleep(.5)
pyautogui.write('C:\\Users\Ali Abdullah\Downloads\BrainFreeMCQS') # Entering New Address
time.sleep(.5)
pyautogui.press('enter')
time.sleep(.5)
pyautogui.click(496,342)
time.sleep(.5)
pyautogui.hotkey("ctrlleft", "a") # Selecting File Name
time.sleep(.5)
pyautogui.press('delete') # Deleting Previous File Name
time.sleep(.5)
pyautogui.write(save_file_name) # Entering New File Name
time.sleep(.5)
pyautogui.click(448,451) # Clicking Save
EDIT 3
当我尝试添加 pyguiauto 代码来保存完整的网页时。我用于 De Selecting Previous CheckBox 的 select_topic.click() 不起作用。 Error: 'WebElement' object is not iterable
import time
import pyautogui
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://brainbooks.pk/newtest-system/free_mcqs.php')
def get_options(driver, class_name):
drop_down = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, class_name)))
select = Select(drop_down)
return select.options
board_option = driver.find_element_by_xpath('//*[@id="board_id"]/option[2]')
board_option.click()
time.sleep(0.5)
class_options = ['//*[@id="class_id"]/option[2]','//*[@id="class_id"]/option[3]','//*[@id="class_id"]/option[4]','//*[@id="class_id"]/option[5]']
for class_select in class_options:
select_class = driver.find_element_by_xpath(class_select)
print(select_class.text)
select_class.click()
time.sleep(3)
if class_select == '//*[@id="class_id"]/option[2]':
try:
subject_options = get_options(driver, 'subject')
except:
select_class = driver.find_element_by_xpath('//*[@id="class_id"]/option[1]')
select_class.click()
select_class = driver.find_element_by_xpath(class_select)
select_class.click()
time.sleep(2)
finally:
subject_options = get_options(driver, 'subject')
for subj in subject_options[1:]:
print(subj.text)
subj.click()
time.sleep(3)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'mutliSelect')))
units_container = driver.find_element_by_class_name("mutliSelect")
topic_options = units_container.find_elements_by_xpath("//input[@type='checkbox']")
time.sleep(1.5)
for select_topic in topic_options:
topic_name = select_topic.find_element_by_xpath("..")
print(topic_name.text)
select_topic.click()
time.sleep(1.5)
generate_button = (driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[1]/div/button[1]')).click()
time.sleep(3)
select_topic.click() #De Selecting Previous CheckBox
time.sleep(1)
view_result_button = (driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div/div[1]/div/button[3]')).click()
# Saving Html Pages
sub_name = subj.text
topic_title = topic_name.text
topic_title = topic_title.replace(':', '-')
topic_title = topic_title.replace(' ', ' ')
save_file_name = sub_name + '-' + topic_title
time.sleep(1)
pyautogui.hotkey("ctrlleft", "s") # Saving File
time.sleep(.5)
pyautogui.press('f4') # Opening Address Bar
time.sleep(.5)
pyautogui.hotkey("ctrlleft", "a") # Selecting Previous Address
time.sleep(.5)
pyautogui.press('delete') # Removing Previous Address
time.sleep(.5)
pyautogui.write('C:\\Users\Ali Abdullah\Downloads\BrainFreeMCQS') # Entering New Address
time.sleep(.5)
pyautogui.press('enter')
time.sleep(.5)
pyautogui.click(496, 342)
time.sleep(.5)
pyautogui.hotkey("ctrlleft", "a") # Selecting File Name
time.sleep(.5)
pyautogui.press('delete') # Deleting Previous File Name
time.sleep(.5)
pyautogui.write(save_file_name) # Entering New File Name
time.sleep(.5)
pyautogui.click(448, 451) # Clicking Save
time.sleep(1.5)
try:
if driver is not None:
driver.quit()
except Exception as e:
print(e)
【问题讨论】:
-
class_options 是什么问题?
-
首先,用英文添加您正在执行的操作的描述。其次,添加您看到的错误消息。三、为什么这么多
break?? -
表单中有四个下拉菜单。我可以选择前 3 个(董事会、班级和主题),然后是单元下拉最后一个。带有复选框的那个。我想循环浏览并一一检查
标签: python selenium selenium-webdriver