题目是这样的:
用selenium写一个测试,检测网页端一个id是"clickMe"的按钮,当它被点击后,弹出一个id是"popUp"的页面,同时之前的主页面在"popUp"页面没有被取消前无法再进行操作
我的解答如下:
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
class TestElement:
def setup_method(self):
self.driver = webdriver.Chrome()
self.driver.get("url")
self.driver.implicitly_wait(5)
def can_not_click(self):
WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable((By.ID, "clickMe")))
self.driver.find_element(By.ID, "clickMe").click()
WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable((By.ID, "popUp")))
try:
self.driver.find_element(By.ID, "clickMe").click()
except ElementClickInterceptedException:
return "element click intercepted: Element is not clickable.Other element would receive the click"
def test_click(self):
msg = self.can_not_click()
assert "element click intercepted" in msg
总感觉哪里不对,求大神探讨
弹出的页面有可能需要进行切换窗口吧
看得出来,你写 try 是为了对应“同时之前的主页面在"popUp"页面没有被取消前无法再进行操作”这句话,但其实那句话的意思是告诉你 “popUp” 在一个弹窗界面,我认为是在考察你 driver.switch_to
的用法
这么答你只是验证了原页面是不是不能操作,这题其实主要就是看你selenium的熟练度的
一个是它既然是个弹窗,那么跟id其实没什么关系,主要是切换到弹窗去验证是否是正确的弹窗,然后才是验证原有页面是不是可以操作,最后还要关心这个弹窗关闭以后原页面是不是正常
是呢,我也觉得没这么简单,可是如何去验证是不是正确的弹窗呢?题目里能看到的验证方式只能是判断弹窗的id可以点击或者可访问之类的(这个id出现了就说明是正确的弹窗?)
另外类似于图片中这种强弹框想操作也需要先switch_to么?
根据它给的条件来判断呗,然后弹窗肯定也是要切换到弹窗的,就是跟普通窗口不一样罢了。有些地方写不出来的有思路就可以,面试并不是看你代码写的多好,主要还是思路,你把关键的东西写出来就行了
补充一句,学了PO需要秀出来。
这个面试题有意思,问的不一般,贴近实用。