gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:46:56 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com