python selenium,提交表单按钮在不同的容器元素选项卡中无效
在此网站)之后,我正在尝试提交表格。到目前为止,如果我不将选项卡更改为“ ntetexpress”,它可以正常工作。
导入
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options
from selenium.common import exceptions as sel_excpt
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument("disable-popup-blocking")
options.add_experimental_option("detach",True)
chrome_driver_pth = ChromeDriverManager().install()
service = ChromeService(executable_path=chrome_driver_pth)
driver = webdriver.Chrome(service=service, options=options)
other_tab = False
url = r"https://www.texpresslanes.com/pricing/average-rates/"
segment_css = "#details-1-toll"
day = "#details-1-day"
time = "#details-1-time"
submit = "#wpcf7-f9-p10-o1 > form > div.form-row.last-row.grid-2 > div > div > input.wpcf7-form-control.wpcf7-submit.button-secondary"
if other_tab:
net_tab= "#content > div.page-layout > div > div > article > div > div > div > div > section.module.module-traffic.module-pyt-tabs.full-width.simple-tabs.inserted > div > div > div.module-tabs > button.tab2"
WebDriverWait(driver, 15).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, net_tab))
)
WebDriverWait(driver, 15).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, net_tab))
).click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, segment_css)))
select_seg = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, segment_css)))
if not other_tab:
select_seg.select_by_value("277")
else:
select_seg.select_by_value("283")
days = driver.find_element(By.CSS_SELECTOR, day_css)
time = driver.find_element(By.CSS_SELECTOR, time_css)
Select(days).select_by_value("monday")
Select(time).select_by_value("00:00:00")
# where it fails if other_tab = True
submit_bttn = driver.find_element(By.CSS_SELECTOR, submit)
submit_bttn.click()
故障消息是超时,或者对于表单提交按钮而言,元素是不可交互的错误。
我尝试过的
更多的预期有条件等待 /等待提交按钮可单击< / p>
执行脚本< / p>
driver.execute("arguments[0].click();",submit_bttn)
# this works when other_tab = False
,所以我实际上我去了网站,在所有中输入必要的字段,并再次使用控制台(在Chrome上)调用
document.querySelector("#wpcf7-f9-p10-o1 > form > div.form-row.last-row.grid-2 > div > div > input.wpcf7-form-control.wpcf7-submit.button-secondary").click()
,它可与LBJ Express的第一个选项卡一起使用,但对于NTE35W选项卡而言不适用于。所以我真的不知道该如何进行。
I'm trying to submit a form after entering the necessary fields on this website. So far it works if I don't change the tab to "NTETEXPRESS".
imports
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options
from selenium.common import exceptions as sel_excpt
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument("disable-popup-blocking")
options.add_experimental_option("detach",True)
chrome_driver_pth = ChromeDriverManager().install()
service = ChromeService(executable_path=chrome_driver_pth)
driver = webdriver.Chrome(service=service, options=options)
other_tab = False
url = r"https://www.texpresslanes.com/pricing/average-rates/"
segment_css = "#details-1-toll"
day = "#details-1-day"
time = "#details-1-time"
submit = "#wpcf7-f9-p10-o1 > form > div.form-row.last-row.grid-2 > div > div > input.wpcf7-form-control.wpcf7-submit.button-secondary"
if other_tab:
net_tab= "#content > div.page-layout > div > div > article > div > div > div > div > section.module.module-traffic.module-pyt-tabs.full-width.simple-tabs.inserted > div > div > div.module-tabs > button.tab2"
WebDriverWait(driver, 15).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, net_tab))
)
WebDriverWait(driver, 15).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, net_tab))
).click()
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, segment_css)))
select_seg = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, segment_css)))
if not other_tab:
select_seg.select_by_value("277")
else:
select_seg.select_by_value("283")
days = driver.find_element(By.CSS_SELECTOR, day_css)
time = driver.find_element(By.CSS_SELECTOR, time_css)
Select(days).select_by_value("monday")
Select(time).select_by_value("00:00:00")
# where it fails if other_tab = True
submit_bttn = driver.find_element(By.CSS_SELECTOR, submit)
submit_bttn.click()
The failure message is either a timeout, or a Element is not interactable error for the form submission button.
Things that I've tried
More expected conditional waits / Waiting for the submit button to be clickable
Executing a script
driver.execute("arguments[0].click();",submit_bttn)
# this works when other_tab = False
So I've actually went to the website, entered in all of the necessary fields, and called using the console (on chrome)
document.querySelector("#wpcf7-f9-p10-o1 > form > div.form-row.last-row.grid-2 > div > div > input.wpcf7-form-control.wpcf7-submit.button-secondary").click()
Again, this works with the first tab for the LBJ express, but not for the NTE35W tab. So I don't really know how to proceed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
提交按钮具有相同的CSS选择器。
只要尝试单击时,都可以在处理任何例外的同时,在处理列表时进行解决。
The submit buttons had the same css selector.
A workaround is to just find_elements and iterate through the list while handling any exception whenever a click is attempted.