python - 在 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()
现在,我无法访问“三”,因为这在新页面上可用。
没有办法解决这个问题吗?
[注:点击“二”后,页面变为“ 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()
推荐阅读
- reactjs - 如何在身份验证流程中导航到其他反应片段中的特定屏幕?
- design-patterns - 如果某个子类不能使用某些策略,如何使用策略设计模式?
- python - 使用正则表达式从熊猫数据框中的列中删除小数点以外的字母和特殊字符无法按预期工作
- flutter - 验证器消息正在触摸用户输入文本
- python - 创建像虚数一样的python类构造函数(a + bj)
- javascript - 更改 nth-child 原生 JavaScript
- email - 如何从外部网络服务器(a1 - 奥地利)获取电子邮件并在我们家中使用它们,以便每个人都有相同的文件结构和信息?
- flutter - 如何根据flutter中的id解析json数据
- vba - 在 VBA 脚本中使用列表框选择的值作为过滤器数组
- tensorflow - 修改局部变量accuracy/total:0