selenium 在Python中选择,IEDriverServer 4.8返回“意外错误”

uplii1fm  于 2023-02-12  发布在  Python
关注(0)|答案(1)|浏览(159)

在IE模式下在Edge浏览器上运行Python代码时,会出现错误:* "无法单击选项元素。执行JavaScript单击函数时返回意外错误,但Internet Explorer的JavaScript引擎无法返回任何错误"*
此代码:

def my_select(self, text):
    self.app.select_from_drop_down('//select[@class="gender"]', text)

叫做这个

def select_from_drop_down(self, selector, text):
    wd = self.wd
    if text is not None:
        Select(wd.find_element(By.XPATH, selector)).select_by_visible_text(text)

在测试中我调用了my_select方法:

def test_select_male(app):
    app.people_page.open_page()
    app.people_page.my_select('Male')

生产日期:

<select class="gender">
                         <option></option>
                         <option name="MALE">Male</option>
                         <option name="FEMALE">Female</option>
                      </select>

这是一个已知的bug,已经报告给了Selenium开发人员,但是还没有修复。https://github.com/SeleniumHQ/selenium/issues/10319人们已经找到了Java的解决方案,并在评论中发表了它。
我曾尝试使用 * execute_script * 编写Python中这个bug的解决方案,但从未成功。

def select_from_drop_down_menu(self):
    btn = wd.find_element(By.XPATH, '//select[@name="MALE"]').click()
    wd.execute_script("arguments[0].click();", btn)

谢谢你的帮忙!

osh3o9ms

osh3o9ms1#

您可以使用execute_script()方法,如下所示:

def select_from_drop_down(self, selector, text):
    wd = self.wd
    if text is not None:
     wd.execute_script("arguments[0].click();", wd.find_element(By.XPATH, selector))

你可以继续称之为:

def test_select_male(app):
    app.people_page.open_page()
    app.people_page.my_select('Male')

并进一步:

def my_select(self):
    self.app.select_from_drop_down('//select[@class="gender"]//option[text()='Male']', text)

相关问题