我对硒很陌生。我正在使用selenium编写一个自动结帐单。脚本执行非常顺利,直到最后签出页。但是,我无法访问最后一页上的元素(‘Razorpay Checkout'),这是一种弹出/iframe窗口。任何帮助访问同样的将是非常感谢的。提前感谢!
下面是我正在使用的代码
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').click()中
文件"C:\Users\hamhatre\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py",行360,在find_element_by_id返回self.find_element(by=By.ID,value=id_)中
文件"C:\Users\hamhatre\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py",第976行,在find_element返回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:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“id=”语言下拉列表“}”(会话信息: chrome=99.0.4844.84)
发布于 2022-04-02 09:15:39
我正在使用色度驱动器版本100。您的代码抛出一个NoSuchElementException。这是因为您的驱动程序试图找到一个不存在的元素。因此,您需要做的是将time.sleep(4)语句放在click()语句之后(浏览器需要时间打开窗口/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()发布于 2022-04-02 17:46:36
在下一个项目中你可以做几件事:
WebDriverWait允许足够的时间来加载元素,switch_to.default_content(),//div[@id="user_address"])。从检查窗口右键单击并复制元素的绝对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 += 1https://stackoverflow.com/questions/71716107
复制相似问题