java - Python 2.7 selenium webdriver 无法读取 java 站点上的表内容

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

我一直在尝试对一个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;">

最佳答案

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

关于java - Python 2.7 selenium webdriver 无法读取 java 站点上的表内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48132621/

相关文章:

java - 尝试在 Android 中打开 Excel 文件时不断收到 FileNotFound 异常

python xlutils : formatting_info=True not yet implemented

android - 如何使用 Sauce Labs 上的 Selenium Automation-Android 模拟器在文本框中输入值

c# - Selenium Chrome C# 更改现有 Web 驱动程序上的用户代理而不创建新驱动程序?

java - java中selenium可以同时操作多个tab吗?

Java 二维数组,查找包含的元素

java - 在什么条件下创建 JSESSIONID?

java - JVM 堆参数

带有负数的 Python 正则表达式问题

python - help() 在哪里找到 Python 中的信息?