在 Selenium 中更改页面后查找元素出现问题 Python
Problem in Find Element after page changed in Selenium Python
我在 python 中使用网络抓取工具库作为 [Selenium]。
我想提交一个表格(没有 AJAX 的多个表格)
所以,我写了这段代码:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://exportgram.com/')
one = driver.find_element_by_xpath('//*[@id="home"]/form/div[1]/div/input[1]')
one.click()
one.send_keys('https://www.instagram.com/p/B6OCVnTglgz/')
two = driver.find_element_by_xpath('//*[@id="home"]/form/div[2]/button')
two.click()
time.sleep(3)
three = driver.find_element_by_xpath('//*[@id="home"]/form/div[2]/button')
three.click()
现在,我无法访问 "three" 因为那在新页面上可用。
没有办法解决这个问题吗?
[注意:点击'two'后,页面变为'https://example.com/media.php']
如果在点击第二个元素后将您重定向到另一个页面,则表示此表单已部分发送。浏览器加载新地址后,您将能够找到新的 DOM 树,因此您必须等待它出现。
您可以通过以下代码执行此操作:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
timeout = 10
three = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH, '//*[@id="home"]/form/div[2]/button')), 'Time out')
three.click()
我在 python 中使用网络抓取工具库作为 [Selenium]。 我想提交一个表格(没有 AJAX 的多个表格)
所以,我写了这段代码:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://exportgram.com/')
one = driver.find_element_by_xpath('//*[@id="home"]/form/div[1]/div/input[1]')
one.click()
one.send_keys('https://www.instagram.com/p/B6OCVnTglgz/')
two = driver.find_element_by_xpath('//*[@id="home"]/form/div[2]/button')
two.click()
time.sleep(3)
three = driver.find_element_by_xpath('//*[@id="home"]/form/div[2]/button')
three.click()
现在,我无法访问 "three" 因为那在新页面上可用。
没有办法解决这个问题吗?
[注意:点击'two'后,页面变为'https://example.com/media.php']
如果在点击第二个元素后将您重定向到另一个页面,则表示此表单已部分发送。浏览器加载新地址后,您将能够找到新的 DOM 树,因此您必须等待它出现。
您可以通过以下代码执行此操作:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
timeout = 10
three = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH, '//*[@id="home"]/form/div[2]/button')), 'Time out')
three.click()