Selenium Python绑定:处理不一致的模态

Selenium Python绑定:处理不一致的模态,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我试图在一个网站上填写表格,该网站根据语言、位置等的不同有不同的版本。我对每个请求都使用相同的firefox配置文件,并且先前选择的信息存储在本地cookie中,因此在第一次选择设置后,模式暂时不会出现然而:它似乎出现不一致,将焦点从表单上移开,并导致elementnotinteractitableexception. 为了增加难度,在页面加载后的一段时间内,模式通常会出现。例如,第一个字段将已填写,然后出现 我的问题是,处理这种模式的最佳方式是什么?我是否可以通过外观捕捉异常原因,检查模态是否

我试图在一个网站上填写表格,该网站根据语言、位置等的不同有不同的版本。我对每个请求都使用相同的firefox配置文件,并且先前选择的信息存储在本地cookie中,因此在第一次选择设置后,模式暂时不会出现然而:它似乎出现不一致,将焦点从表单上移开,并导致
elementnotinteractitableexception.

为了增加难度,在页面加载后的一段时间内,模式通常会出现。例如,第一个字段将已填写,然后出现

我的问题是,处理这种模式的最佳方式是什么?我是否可以通过外观捕捉异常原因,检查模态是否存在,然后继续填充表单字段?还是有更好的解决方案

谢谢你的帮助

到目前为止,我尝试过的代码是:

url = "https://www.aircanada.com/ca/en/aco/home.html"
control_profile = webdriver.FirefoxProfile('/path/to/my/profile')
browser_control = webdriver.Firefox(control_profile)

browser_control.get(url)

# To deal with the modal, but obviously fails when it is not present
browser_control.find_element_by_id('enCAEdition').click()

# two text fields I tried to fill out, as a sanity check
departure = browser_control.find_element_by_id('origin_focus_0')
departure.send_keys("my departure location")
departure.send_keys(Keys.RETURN)

destination = browser_control.find_element_by_id('destination_label_0')
destination.send_keys("my destination")
destination.send_keys(Keys.RETURN)

您可以等待一段时间,直到模态出现,关闭它并处理表单:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

url = "https://www.aircanada.com/ca/en/aco/home.html"
control_profile = webdriver.FirefoxProfile('/path/to/my/profile')
browser_control = webdriver.Firefox(control_profile)

browser_control.get(url)

# Wait up to 10 seconds until modal appears to close it
try:
    wait(browser_control, 10).until(EC.element_to_be_clickable(('xpath', '//button[text()="Confirm | Confirmer"]'))).click()
# If modal didn't appear- just continue
except TimeoutException:
    pass

departure = browser_control.find_element_by_xpath('//input[@placeholder="FROM"]')
browser_control.execute_script('arguments[0].setAttribute("class","glyph-input glyph-left-input form-control ng-pristine ng-valid ng-touched");', departure)
departure.send_keys("Berlin")
wait(browser_control, 5).until(EC.visibility_of_element_located(("xpath", "(//div[@class='location-primary']/span)[1]")))
departure.send_keys(Keys.RETURN)

destination = browser_control.find_element_by_xpath('//input[@placeholder="TO"]')
browser_control.execute_script('arguments[0].setAttribute("class","glyph-input glyph-left-input form-control ng-pristine ng-valid ng-touched");', destination)
destination.send_keys("Oslo")
wait(browser_control, 5).until(EC.visibility_of_element_located(("xpath", "(//div[@class='location-primary']/span)[4]")))
destination.send_keys(Keys.RETURN)

你能分享页面
URL
和你迄今为止尝试过的代码吗?当然,这是页面:,我将把我的代码附加到问题中。我包括了两个简单的例子,试图填充一些文本字段,但由于模式错误而失败。通常,第一个字段填写正确,但在发送报税表之前失败。否则,当模态无法显示时,它将失败,并且第一个按钮元素不可见。感谢您的回答,效果很好,因此我将其标记为已接受。尽管如此,我开始认为我的问题其实是在报税表发出的时候。当我实际键入一个位置并按enter键时,下拉列表中的第一个元素将被自动选择。但出于某种原因,它似乎在脚本中失败了。你对解决这个问题有什么见解吗?我只是注意到,在焦点中,这个特殊的div添加了一个类“hidden”,但是由于缺少实际的输入元素,我不知道还能用什么。再次感谢你的快速回答。我尝试了一种不同的方法,发送键,等待下拉列表出现,然后通过xpath选择并单击第一个元素。到目前为止似乎有效,但如果我遇到麻烦,我会试试你的解决办法