【发布时间】:2020-04-23 08:31:39
【问题描述】:
感谢所有看到此问题的人。我点击一个按钮来触发一个函数的执行,这个函数使用selenium在这个函数中创建一个driver。
有了这个驱动,我可以在不打开火狐浏览器窗口的情况下拖放图片、点击按钮、发送短信验证码到手机。
当一切都结束后,我需要跳转一个向导让我的用户输入发送到手机的验证码。但是当我们从 odoo 界面加载向导并输入验证码并单击确认按钮后,我需要输入我刚刚用driver 打开的页面中的验证码值。
如何获取之前的driver?有什么好的解决方案,谢谢。
这是我要使用的函数selenium
# -*- coding: utf-8 -*-
from odoo import models, fields, api, _
import selenium
from selenium.webdriver.firefox.options import Options
import time
from selenium.webdriver import ActionChains
class SaleOrderSend(models.Model)
_name='sale.order.send'
@api.multi
def handel_web_send_captcha(self):
options = Options()
options.add_argument('--headless')
driver = selenium.webdriver.Firefox(options=options)
driver.get(sign_url)
time.sleep(15)
ac1 = driver.find_elements_by_class_name("es-drag-seal")[0]
ac2 = driver.find_element_by_class_name('es-sign-date-field')
time.sleep(1)
ActionChains(driver).drag_and_drop(ac1, ac2).perform()
time.sleep(2)
submit_button = driver.find_element_by_xpath("//button//span[text()='Submit']")
submit_button.click()
time.sleep(3)
driver.switch_to.frame(0)
short_message = driver.find_element_by_xpath("//a[text()='Sign']")
short_message.click()
time.sleep(3)
send_code = driver.find_element_by_xpath("//div[@class='el-form-item__content']//button//span[text()='Get_Code']")
send_code.click()
time.sleep(2)
driver.close()
action = {
'name': _("Please enter the verification code"),
'type': 'ir.actions.act_window',
'res_model': 'signature.code',
'view_type': 'form',
'view_mode': 'form',
'target': 'new'
}
return action
这是我在向导中的 py 文件和 xml 文件
# -*- coding: utf-8 -*-
from odoo import models, fields, api, _
import selenium
from selenium.webdriver.firefox.options import Options
import time
from selenium.webdriver import ActionChains
class SignatureCode(models.Model):
_name = 'signature.code'
_description = 'Signature Code'
sign_code = fields.Char('Sign Code')
sign_url = fields.Char('Sign Url')
@api.multi
def confirm_sign_code(self):
"""
Enter the user's captcha code into the browser page you just opened using selenium.
:return:
"""
sign_code = self.sign_code
# How do I get to the previous driver? driver = selenium.webdriver.Firefox(options=options)
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="signature_code_form_view" model="ir.ui.view">
<field name="name">Signature Code Form View</field>
<field name="model">signature.code</field>
<field name="arch" type="xml">
<form string="Signature Code Form View">
<sheet>
<group>
<group>
<field name="sign_code" required="1" readonly="0"/>
</group>
</group>
</sheet>
<footer>
<button string="Confirm" name="confirm_sign_code" type="object" default_focus="1"
class="btn-primary"/>
<button string="Cancel" class="btn-default" special="cancel"/>
</footer>
</form>
</field>
</record>
</odoo>
【问题讨论】:
标签: python selenium odoo selenium-firefoxdriver