【问题标题】:Python 2.7 selenium webdriver failing to read table contents on java sitePython 2.7 selenium webdriver 无法读取 java 站点上的表内容
【发布时间】:2018-06-16 09:19:35
【问题描述】:

我一直在尝试对一个 java 网站进行网页抓取(对我来说是新的),但每次我尝试从其主表中读取数据时它都会失败。我知道我要搜索的元素在网站上,所以我不知道是什么导致找不到该元素。我可以搜索其他领域,但由于某种原因,我无法读取表数据(没有具有相同类名称的元素,但我已将站点包含在下面的代码中)。有人可以帮助我了解我可能缺少什么吗?

注意:该网站需要用户名/密码组合,我可以顺利登录。

我的代码如下:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import sys, os, requests
from os import system

def main():
    file = open("wbSc2.txt","w")
    print 'starting...'
    print >> file, 'starting...'
    site2 = "https://www.oddsmonkey.com/Tools/Oddsmatcher.aspx"
    driver = webdriver.Firefox()
    print 'grabbing site'
    print >> file, 'grabbing site'
    driver.get(site2)
    driver.implicitly_wait(2)
    user = driver.find_element_by_id("dnn_ctr433_Login_Login_DNN_txtUsername")
    password = driver.find_element_by_id("dnn_ctr433_Login_Login_DNN_txtPassword")
    user.send_keys('myusername')
    password.send_keys('mypassword')
    submit = driver.find_element_by_id("dnn_ctr433_Login_Login_DNN_cmdLogin")
    submit.click()
    time.sleep(3)
    close = driver.find_element_by_xpath("//button[@class='rltbActionButton rltbCloseButton']") 
    close.click()
    driver.implicitly_wait(10)
    try:
        print 'attempting to find the table'
        print >> file, 'attempting to find the table'
        table = driver.find_element_by_xpath("//table[@id='RAD_SPLIITER_dnn_ctr956_View_RadSpliter1']")
        print 'successfully found table'
        print >> file, 'attempting table find'
        print table.text
        print >> file, table.text
    except:
        print 'failed to find table'
        print >> file, 'failed to find table'

    try:
        print 'attempting to find row'
        print >> file, 'attempting to find row'
        row = table.find_element_by_xpath('tr')
        print 'successfully found row'
        print >> file, 'successfully found row'
        print row.text
        print >> file, row.text
    except:
        print 'failed to find row'
        print >> file, 'failed to find row'
    driver.close()
    system("pause") 

main()

我一直找不到表,我不知道为什么,因为它存在于网站上,如其源代码所示:

<table id="RAD_SPLITTER_dnn_ctr956_View_RadSplitter1" class="RadSplitter RadSplitter_Telerik" style="width:1px;height:1px;border-left-width:1px;border-top-width:1px;">

【问题讨论】:

    标签: java python-2.7 selenium web-scraping selenium-firefoxdriver


    【解决方案1】:

    确保您不在防火墙后面。我遇到了同样的问题,在运行 print(driver.page_source) 行后,我意识到我的驱动程序读取的源与我通常在常规浏览器上读取的源不同。如果您确定您引用了正确的 xpath,那么值得一试,看看您的驱动程序实际上是从什么中提取出来的。 (从一个菜鸟到另一个菜鸟:))

    【讨论】:

    • 感谢您的帮助!当我添加那行时,似乎什么都没有打印出来。
    • 尝试在 close.click() 之后添加 time.sleep(30),如果这样,则意味着页面需要更多时间来加载。仅供参考,implicitly_wait() 是设置 selenium 查找元素的最大搜索时间,如果在其中找不到元素,selenium 将报告 NoSuchElement Exception,而不是设置页面加载超时。
    • @Chris Brocious 我接受了你的回答,因为它直接把我带到了 yong 刚刚提供帮助的问题上。这是计时和打印 page_source 的混合。第一个问题是列出了“l”的属性,我输入了“1”。即使在那之后,时机也让我错过了某些行动。非常感谢你们!这让我发疯了!
    猜你喜欢
    • 2022-06-11
    • 2021-04-28
    • 1970-01-01
    • 2022-12-14
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多