python 按钮似乎无法通过 selenium 点击

kknvjkwl  于 2023-05-16  发布在  Python
关注(0)|答案(2)|浏览(137)

我一直试图点击NAS网站的提交按钮,但自从网站上次更新后,我的代码就不再工作了,无法点击提交按钮。

from selenium import webdriver
import time

# Opens NAS Website
driver = webdriver.Chrome()
driver.get("https://www.northamericanstainless.com/NAS_App/Surcharge1?language=E&type=F")
time.sleep(5)

# Click on Submit button
Submit = driver.find_element("xpath", '//*[@id="submitStylev2"]')
Submit.click()

# Wait time
time.sleep(5)

# Switches to pricing page
popup_handle = driver.window_handles[1]
driver.switch_to.window(popup_handle)

它应该点击了网站上的提交按钮。在网站进行升级之前,此代码可以正常工作。This is the error I am receiving.

ruoxqz4g

ruoxqz4g1#

我认为在更新网站后,他们改变了按钮的位置,我认为你应该改变提交项目的xpath,就像在错误中一样,你的代码无法找到提交按钮,请尝试检查按钮的位置。或者这不起作用,请尽量不要使用xpath。希望能解决你的问题

uplii1fm

uplii1fm2#

您不能再单击Submit按钮的原因是Submit按钮存在于iframe中。
要访问它,你需要先切换到这个iframe,然后你就可以找到它来执行点击。
你可以这么做

import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC

driver = Chrome()

driver.get("https://www.northamericanstainless.com/NAS_App/Surcharge1?language=E&type=F")

# wait for the iframe element to get loaded/located on the page
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'iframe.surcharge-iframe')))

# switch to the desired iframe 
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe.surcharge-iframe'))

# click on submit button
driver.find_element(By.ID, 'submitStylev2').click()
time.sleep(5)

相关问题