Selenium-select选框报错(element is not attached to the page document)记录

记录一下自动化测试中遇到的select选框点击问题

1.先贴代码

#打开谷歌浏览器
driver=webdriver.Chrome();
#driver.fullscreen_window();
#输入百度网站
driver.get('https://www.google.com/');
#driver.find_element_by_xpath("//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input").send_keys('新冠状病毒');
time.sleep(1);
#点击手气不错
driver.find_element_by_css_selector("#tsf > div:nth-child(2) > div.A8SBwf > div.FPdoLc.tfB0Bf > center > input.RNmpXc").click();
time.sleep(1);
#点击关于
driver.find_element_by_css_selector("#nav-list > li:nth-child(2) > a").click();
time.sleep(1);
#循环点击select选框
for index in range(len(driver.find_elements_by_tag_name("option"))):
    driver.refresh();
    select = driver.find_element_by_id("lang-chooser");
    select_value = select.find_elements_by_tag_name("option");
    time.sleep(2);
    select_value[index].click();
    time.sleep(2);

首先通过by_tag_name方法获取select选框中的option选项信息,是以List的方式返回的

driver.find_elements_by_tag_name("option")

然后用for循环,通过索引号遍历select选项的List,去click()当前选项
由于每次点击select选项是会刷新整个网页,会导致报错

element is not attached to the page document

所以要用到driver.refresh()方法刷新当前页面,然后再去重新获取一次select选项框的元素去点击

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值