【问题标题】:Fetch data from hidden table using Selenium python使用 Selenium python 从隐藏表中获取数据
【发布时间】:2020-10-05 09:42:28
【问题描述】:

我正在尝试从网站获取电台信息: https://indiarailinfo.com/atlas

我要获取的表格是一个隐藏表格,仅在文本框中输入文本时才会出现 Hidden table 下面是包含所需表格的div的代码(table class='dropdowntable")

<div style="top: 165px; left: 721px; display: none;" class="list hideslow">
<span style="display:none">LappGetStationList/nzm/0/1/0?&amp;date=1601890451781&amp;seq=9</span>
<table class="dropdowntable" numrows="1" cellspacing="0"><tbody><tr class="rowM1" rownum="0"><td style="display:none">748</td><td class="rcol" nowrap=""><span><span class="listmatch">NZM</span></span></td><td class="icol" nowrap=""><span>Hazrat Nizamuddin</span></td><td class="jcol" nowrap=""><span>NR-Old Delhi Div</span></td><td style="display:none;">NZM/Hazrat Nizamuddin</td></tr><tr class="rowm2" rownum="0"><td style="display:none">748</td><td><span nowrap="">&nbsp;</span></td><td colspan="2" nowrap=""><span>Nizamuddin East/Sarai KaleKhan Bus Terminal 110013 Delhi NCT</span></td><td style="display:none">NZM/Hazrat Nizamuddin</td></tr><tr><td class="listfooter" colspan="5"><span width="10%" class="listfooterhidden">prev</span>&nbsp;&nbsp;<span width="10%" class="listfooterhidden">next</span>&nbsp;&nbsp;</td></tr><tr><td>&nbsp;</td></tr></tbody></table>

</div>

我曾尝试按班级搜索表格,但没有奏效。欢迎提出任何建议。

更新 1:

这是我正在使用的代码:

driver = webdriver.Chrome()    
driver.get('https://indiarailinfo.com/atlas')
searchBox = driver.find_element(By.ID, 'TrkStnListBox')
searchBox.send_keys(code)
searchBox.click()

我尝试过的方法:

1.dropDownTable = driver.find_element(By.PARTIAL_LINK_TEXT,'LappGetStationList')
2.dropDownTable = driver.find_element(By.CLASS_NAME,'list hideslow')

现在以下是我遇到问题的地方:

# hidden = driver.find_element(By.XPATH,"//input[@id='']")
driver.execute_script("arguments[0].setAttribute('style','visibility:visible;');", hidden)
driver.execute_script("arguments[0].click();", dropDownTable)
dropDownTableBody = dropDownTable.find_element(By.TAG_NAME,'tbody')
for entry in dropDownTableBody.find_elements(By.TAG_NAME,'tr'):
        if entry.get_attribute('rownum')==0 and entry.get_attribute('class')=='rowM1':
            for item in entry.find_elements(By.TAG_NAME,'td'):
                if item.get_attribute('class')=='icol':
                    station.update({
                        'STATION NAME' : decodeText(item)
                    })
                if item.get_attribute('class')=='jcol':
                    list = decodeText(item).split('-')
                    division = list[1][:list[1].find('Div')]
                    station.update({
                        'RAILWAY ZONE' : list[0],
                        'RAILWAY DIVISION' : division
                    })
            print(station)

【问题讨论】:

  • 您的代码将有助于确定问题所在。谢谢

标签: python selenium


【解决方案1】:

我已尝试使用chrome 浏览器希望这也适用于FF 浏览器。

在搜索框中插入文本后,您需要等待表格填充它是自动建议框。

诱导WebDriverWait()并等待visibility_of_element_located() 一些表格数据是隐藏的,所以改为文本使用get_attribute("textContent")

代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver=webdriver.Chrome()
driver.get("https://indiarailinfo.com/atlas")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"TrkStnListBox"))).send_keys("nzm")
table=WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'table.dropdowntable')))
for row in table.find_elements_by_xpath("./tbody/tr")[:2]:
    for col in row.find_elements_by_xpath("./td"):
        print(col.get_attribute("textContent"))

控制台输出:

748
NZM
Hazrat Nizamuddin
NR-Old Delhi Div
NZM/Hazrat Nizamuddin
748
 
Nizamuddin East/Sarai KaleKhan Bus Terminal 110013 Delhi NCT
NZM/Hazrat Nizamuddin

希望这就是你所追求的。

【讨论】:

  • 谢谢!!这就像一个魅力。摇头,为什么不考虑实施等待。
【解决方案2】:

我能够用

获取表格
# Go to the website
driver = webdriver.Firefox()
driver.get("https://indiarailinfo.com/atlas")

# Send the search term
driver.find_element(By.ID, "TrkStnListBox").send_keys("nzm")
time.sleep(1)

# Read the Table
el = driver.find_element(By.CLASS_NAME, "dropdowntable")

但即使在el 变量中搜索“td”和“span”标签后,我也无法访问表中的信息。也许有人可以从这里拿走。

编辑:添加了睡眠注释以确保该表已准备就绪。

【讨论】:

  • 这对你有用吗?我无法找到元素
  • 是的,它对我有用。你能看到一个 Firefox(或 Chrome,如果你正在使用它)窗口打开并将“nzm”写入搜索框吗?
  • 嗯,这不完全是“工作”,获取表格会引发元素未找到异常,其他东西确实有效,是的。
  • 有时有效,有时无效。我添加了一条睡眠评论来解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 2019-08-06
  • 2020-12-16
  • 2021-08-21
  • 2021-06-15
  • 1970-01-01
  • 2017-07-05
相关资源
最近更新 更多