python - Selenium 不会在看起来像弹出窗口的地方找到元素
问题描述
对于这个项目,我正在尝试寻找某个地区的信用合作社。当我搜索地址并获取该地区的信用合作社列表时,selenium 不会找到要打印它们的元素。我已经尝试过这两种方法,但它们都不起作用。有什么我想念的吗?我读过关于动作链的文章,但我不太确定它们是如何工作的,或者它们是否适用于此。谢谢!
from selenium import webdriver
address = 'anything'
#Go to link to get all credit unions in the area
driver.get('https://mapping.ncua.gov/')
driver.find_element_by_xpath('/html/body/div[1]/form/div[3]/div[2]/div[1]/div/div[2]/input[1]').send_keys(address)
driver.find_element_by_xpath('/html/body/div[1]/form/div[3]/div[2]/div[1]/div/div[2]/input[2]').click()
# Method #1
#Print the name of the credit unions by using a loop and finding the class name
names = driver.find_elements_by_class_name('name')
for name in names:
print(name.text)
# Method #2
#Print the name of the credit unions by using a loop and finding the xpath
for y in range(1, 10):
y = str(y)
name = driver.find_element_by_xpath('/html/body/div[1]/form/div[3]/div[2]/div[1]/div/div[2]/div[6]/div[1]/div[1]/table/tbody/tr['+y+']/td[1]/a/span[1]').text
print(name)
解决方案
我想通了...我只需要等待 2 秒钟即可加载页面。现在,两种查找元素的方法都可以正常工作。
推荐阅读
- shell - Shell:带有 echo 命令的 ksh 脚本不显示任何内容
- regex - 应用程序脚本正则表达式:SyntaxError:量词无效
- javascript - 正则表达式,将每个 CSV 字段用双引号括起来
- c# - datagridtextcolumn.ElementStyle 上的 WPF 数据网格绑定错误
- java - 将范围报告 HTML 文件转换为 PDF
- c++ - 模板变量是否允许在多个翻译单元中并有效合并?
- android - 多窗口 - 是否可以让应用程序小于 33% 的屏幕?
- c# - 我可以从本地 C# 应用程序列出 Azure 资源组吗?
- python - Python - 将字典添加到列表
- java - 如何使用 JDBC 将用户在 jsp 输入类型“时间”中输入的值插入 MYSQL 数据库