本篇博客主要为 https://bbs.csdn.net/skill/python 频道练习题模块补充题目,暂定每天提供 5 or 6 道测试题,后面可能会更多哦~。
本篇博客对【爬虫】→**【Selenium】**进行出题。
以下题目,默认将正确答案,放置在选项 A 位置
题目难度:3 星
题干(问题描述):
使用 selenium 框架,实现捕获浏览器弹窗代码,正确的是?
选项 A:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.csdn.net")
time.sleep(2)
js = "alert('提示弹窗')"
driver.execute_script(js)
alt = driver.switch_to.alert
print(alt.text)
选项 B:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.csdn.net")
time.sleep(2)
js = "alert('提示弹窗')"
alt = driver.switch_to.alert
print(alt.text)
选项 C:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.csdn.net")
time.sleep(2)
js = "alert('提示弹窗')"
driver.execute_script(js)
alt = driver.switch_to.frame
print(alt.text)
选项 D:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.csdn.net")
time.sleep(2)
js = "alert('提示弹窗')"
driver.execute_script(js)
alt = driver.switch_to.window
print(alt.text)
正确答案:A
题目难度:1 星
题干(问题描述):
在 Selenium 中如何打开无头谷歌浏览器
选项 A:
from selenium import webdriver
opt = webdriver.ChromeOptions()
opt.headless = True
driver = webdriver.Chrome(options=opt)
driver.get("http://www.csdn.net")
选项 B:
from selenium import webdriver
opt = webdriver.ChromeOptions()
opt.add_argument('-headless')
driver = webdriver.Chrome(options=opt)
driver.get("http://www.csdn.net")
选项 C:
from selenium import webdriver
opt = webdriver.ChromeOptions()
opt.headless = False
driver = webdriver.Chrome(options=opt)
driver.get("http://www.csdn.net")
选项 D:
from selenium import webdriver
opt = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=opt)
driver.get("http://www.csdn.net")
正确答案:A
题目难度:2 星
题干(问题描述):
获取 Selenium 打开的所有窗口句柄,并切换浏览器到第一个选项卡。
选项 A:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.csdn.net")
all_handles = driver.window_handles
print(all_handles)
driver.switch_to.window(all_handles[0])
选项 B:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.csdn.net")
all_handles = driver.window_handles
print(all_handles)
driver.switch_to.window(all_handles[-1])
选项 C:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.csdn.net")
all_handles = driver.window_handles
print(all_handles)
driver.switch_to.window(all_handles[1])
选项 D:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.csdn.net")
all_handles = driver.window_handles
print(all_handles)
driver.switch_to.window(all_handles[0:])
正确答案:A
题目难度:2 星
题干(问题描述):
selenium 可以隐式设置网页等待时间,下述那一段代码可以实现隐式等待 10 秒。
选项 A:
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://www.csdn.net")
选项 B:
import time
from selenium import webdriver
driver = webdriver.Chrome()
time.sleep(10)
driver.get("http://www.csdn.net")
选项 C:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.csdn.net")
locator = (By.TAG_NAME, 'title')
WebDriverWait(driver, 10).until(ec.presence_of_element_located(locator))
选项 D:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.csdn.net")
locator = (By.TAG_NAME, 'title')
WebDriverWait(driver, 0.5,10).until(ec.presence_of_element_located(locator))
正确答案:A
题目难度:3 星
题干(问题描述):
Selenium 在执行 Javascript 代码时,可以向Javascript 代码中传递参数,下述写法正确的是。
选项 A:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
driver = webdriver.Chrome()
driver.get("http://www.csdn.net")
js = "document.title = arguments[0]"
ret = driver.execute_script(js, ("我的CSDN",))
选项 B:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
driver = webdriver.Chrome()
driver.get("http://www.csdn.net")
js = "document.title = args[0]"
ret = driver.execute_script(js, ("我的CSDN",))
选项 C:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
driver = webdriver.Chrome()
driver.get("http://www.csdn.net")
js = "document.title = arguments[0]"
ret = driver.execute_script(("我的CSDN",), js)
选项 D:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
driver = webdriver.Chrome()
driver.get("http://www.csdn.net")
js = "document.title = arguments[0]"
ret = driver.execute_script("我的CSDN", js)
正确答案:A
https://codechina.csdn.net/hihell/question