首页 > 解决方案 > 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)
    

标签: pythonpython-3.xseleniumpopupelement

解决方案


我想通了...我只需要等待 2 秒钟即可加载页面。现在,两种查找元素的方法都可以正常工作。


推荐阅读