无法使用 selenium python 访问弹出窗口/iframe window

Unable to access Pop Up / iframe window using selenium python

我对硒还很陌生。我正在使用 selenium python 制作自动结账凭证。脚本执行非常顺利,直到最后的结帐页面。但是,我无法访问最后一页上的元素 ('Razorpay Checkout'),这是一种 pop up/iframe window。 访问相同的任何帮助将不胜感激。 提前致谢!

下面是我使用的代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome('C:/Users/hamhatre/Desktop/chromedriver.exe')
driver.get("https://shopatsc.com/collections/playstation-5/products/ps5-horizon-forbidden-west")
driver.maximize_window()
driver.implicitly_wait(20)
elem = driver.find_element_by_xpath('//*[@id="pincode_input"]')
elem.send_keys("400708")
driver.find_element_by_xpath('//*[@id="check-delivery-submit"]').click()
driver.find_element_by_xpath('/html/body/div[2]/main/div[1]/div/div/div/div[2]/div[1]/form/div[3]/div/button[2]').click()
driver.find_element_by_xpath('//*[@id="checkout_email"]').send_keys('abc@gmail.com')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_first_name"]').send_keys('abc')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_last_name"]').send_keys('xyz')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_address1"]').send_keys('Rd1, Flat no 2, Apt 1')
driver.find_element_by_xpath(('//*[@id="checkout_shipping_address_address2"]')).send_keys('Nothing')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_city"]').send_keys('Mumbai')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_province"]').send_keys('Maharashtra')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_zip"]').send_keys('400708')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_phone_custom"]').send_keys('9876543210')

driver.find_element_by_id('continue_to_shipping_button_custom').click()
driver.find_element_by_id('continue_button').click()
driver.find_element_by_xpath('/html/body/div/div/div/main/div[1]/div/form/div[3]/div[1]/button').click()


print(driver.title)
seq = driver.find_elements_by_tag_name('iframe')
print(seq)
print("No of frames present in the web page are: ", len(seq))


for index in range(len(seq)):
    iframe = driver.find_elements_by_tag_name('iframe')[index]
    driver.switch_to.frame(iframe)
    driver.find_element_by_id('language-dropdown').click()

错误如下

追溯(最近调用最后):

文件“C:\Users\hamhatre\Desktop\Algotron\WebScrap_Sample.py”,第 47 行,在 中 driver.find_element_by_id('language-dropdown').点击()

文件“C:\Users\hamhatre\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 360 行,在 find_element_by_id return self.find_element(by=By.ID, value=id_)

文件“C:\Users\hamhatre\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 976 行,在 find_element return self.execute(Command.FIND_ELEMENT, {

文件“C:\Users\hamhatre\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 321 行,在执行中 self.error_handler.check_response(回应)

文件“C:\Users\hamhatre\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py”,第 242 行,在 check_response 提高 exception_class(消息、屏幕、堆栈跟踪)

NoSuchElementException: 没有这样的元素:无法定位元素:{"method":"css selector","selector":"[id="language-dropdown"]"} (会话信息:chrome=99.0.4844.84)

我使用的是 chromedriver 版本 100。您的代码抛出 NoSuchElementException。那是因为您的驱动程序试图找到一个不存在的元素。因此,您需要做的是在 click() 语句之后放置 time.sleep(4) 语句(您的浏览器需要时间来打开 window/s)。如果您已完成使用驱动程序,请使用 driver.quit().

以下代码对我有用:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome('C:/Users/hamhatre/Desktop/chromedriver.exe')
driver.get("https://shopatsc.com/collections/playstation-5/products/ps5-horizon-forbidden-west")
driver.maximize_window()
driver.implicitly_wait(20)
elem = driver.find_element_by_xpath('//*[@id="pincode_input"]')
elem.send_keys("400708")
driver.find_element_by_xpath('//*[@id="check-delivery-submit"]').click()
driver.find_element_by_xpath('/html/body/div[2]/main/div[1]/div/div/div/div[2]/div[1]/form/div[3]/div/button[2]').click()
driver.find_element_by_xpath('//*[@id="checkout_email"]').send_keys('abc@gmail.com')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_first_name"]').send_keys('abc')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_last_name"]').send_keys('xyz')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_address1"]').send_keys('Rd1, Flat no 2, Apt 1')
driver.find_element_by_xpath(('//*[@id="checkout_shipping_address_address2"]')).send_keys('Nothing')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_city"]').send_keys('Mumbai')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_province"]').send_keys('Maharashtra')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_zip"]').send_keys('400708')
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_phone_custom"]').send_keys('9876543210')

driver.find_element_by_id('continue_to_shipping_button_custom').click()
time.sleep(4)
driver.find_element_by_id('continue_button').click()
time.sleep(4)
driver.find_element_by_xpath('/html/body/div/div/div/main/div[1]/div/form/div[3]/div[1]/button').click()
time.sleep(4)


print(driver.title)
seq = driver.find_elements_by_tag_name('iframe')
print(seq)
print("No of frames present in the web page are: ", len(seq))


for index in range(len(seq)):
    iframe = driver.find_elements_by_tag_name('iframe')[index]
    driver.switch_to.frame(iframe)
    driver.find_element_by_id('language-dropdown').click()

您可以在下一个项目中做的几件事:

  • 使用WebDriverWait为加载元素留出足够的时间
  • 在切换到另一帧之前使用switch_to.default_content()
  • 切换帧时使用 time.sleep()
  • 尝试使用相对 xpath(例如://div[@id="user_address"])。右键单击并从检查 window 复制元素的绝对 xpath 很容易,但是如果在两者之间添加了另一个元素,您的 xpath 将不再指向所需的元素。此外,相对 xpath 将使您有更深入的理解,让您轻松找到元素。

此代码应该有效:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException
import time

driver = webdriver.Chrome('D://chromedriver/100/chromedriver.exe')
driver.get("https://shopatsc.com/collections/playstation-5/products/ps5-horizon-forbidden-west")
driver.maximize_window()

wait = WebDriverWait(driver, 20)


wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="pincode_input"]'))).send_keys('400708')
wait.until(EC.presence_of_element_located((By.XPATH, '//span[@id="check-delivery-submit"]'))).click()
wait.until(EC.presence_of_element_located((By.XPATH, '//button[normalize-space(text())="Buy Now"]'))).click()
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="checkout_email"]'))).send_keys('abc@gmail.com')
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="checkout_shipping_address_first_name"]'))).send_keys('abc')
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="checkout_shipping_address_last_name"]'))).send_keys('xyz')
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="checkout_shipping_address_address1"]'))).send_keys('Rd1, Flat no 2, Apt 1')
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="checkout_shipping_address_address2"]'))).send_keys('Nothing')
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="checkout_shipping_address_city"]'))).send_keys('Mumbai')
wait.until(EC.presence_of_element_located((By.XPATH, '//select[@id="checkout_shipping_address_province"]'))).send_keys('Maharashtra')
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="checkout_shipping_address_zip"]'))).send_keys('400708')
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@id="checkout_shipping_address_phone_custom"]'))).send_keys('9876543210')
wait.until(EC.presence_of_element_located((By.XPATH, '//button[@id="continue_to_shipping_button_custom"]'))).click()
wait.until(EC.presence_of_element_located((By.XPATH, '//button[@id="continue_button"]'))).click()
wait.until(EC.presence_of_element_located((By.XPATH, '//span[normalize-space(text())="Complete order"]/parent::button'))).click()


print(driver.title)

# finding all iframes
iframes = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'iframe')))
print(f'No. of iframes: {len(iframes)}')

print('Looping through frames to check language dropdown')

found = False
frameno = 0
tries = 1
maxtries = 5
# Attempt at least 5 times to click on the language dropdown
while not found and frameno < len(iframes) and tries <= maxtries:
    try:
        print(f"Frame# {frameno} \t {tries} of {maxtries} tries")
        print('Switching to frame')
        driver.switch_to.frame(iframes[frameno])
        time.sleep(2)
        wait.until(EC.presence_of_element_located((By.XPATH, '//div[@id="language-dropdown"]/div/div'))).click()
        found = True
        print('Found and clicked')
        
    except StaleElementReferenceException:
        print("Exception occured. Capturing iframes again")
        driver.switch_to.default_content()
        iframes = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'iframe')))
        
    except Exception as ex:
        print("Couldn't find/click on the language dropdown")
        frameno += 1
        
    finally:
        print('Switching to default content')
        driver.switch_to.default_content()
        time.sleep(2)
        tries += 1