Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何找到含硒的对象?_Python_Selenium_Python 2.7 - Fatal编程技术网

Python 如何找到含硒的对象?

Python 如何找到含硒的对象?,python,selenium,python-2.7,Python,Selenium,Python 2.7,这是我的代码,我正在更改、修复、尝试另一个模块,但仍然无法用提取的div文本获得响应 import selenium from selenium import webdriver driver = webdriver.Firefox() driver.get('http://www.helloworld.com/') element = driver.find_element_by_id('main') WebElement = driver.findElement(By.xpath("

这是我的代码,我正在更改、修复、尝试另一个模块,但仍然无法用提取的div文本获得响应

import selenium 
from selenium import webdriver

driver = webdriver.Firefox() 
driver.get('http://www.helloworld.com/')
element = driver.find_element_by_id('main')

WebElement = driver.findElement(By.xpath("//div[@class='main']"));
webElement.getText();
我试着使用bs4软件包,但有一个大问题,因为我想要的数据只有在我登录网站时才可能得到,在bs4中,响应就像来自来宾帐户,没有登录

以下是我使用Selenium代码得到的回溯:

Traceback (most recent call last):
  File "D:/Python27/get text value div.py", line 8, in <module>
    WebElement = driver.findElement(By.xpath("//div[@class='main']"));
AttributeError: 'WebDriver' object has no attribute 'findElement'
shell中打印元素
后的respond。文本

回溯(最近一次呼叫最后一次):

文件“”,第1行,在
element.text
文件“D:\Python27\lib\selenium\webdriver\remote\webelement.py”,第50行,文本
返回self.\u执行(Command.GET\u元素\u文本)['value']
文件“D:\Python27\lib\selenium\webdriver\remote\webelement.py”,第228行,在\u execute中
返回self.\u parent.execute(命令,参数)
文件“D:\Python27\lib\selenium\webdriver\remote\webdriver.py”,第165行,在execute中
self.error\u handler.check\u响应(响应)
文件“D:\Python27\lib\selenium\webdriver\remote\errorhandler.py”,第152行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
WebDriverException:消息:u'\'[JavaScript错误:“a为空”{file:file:///c:/dokume~1/tomek/lokale%20einstellungen/temp/tmpupvgr2/extensions/fxdriver@googlecode.com/调用方法[nsicomandprocessor::execute]时的components/command\u processor.js“line:7623}]\'

以下内容是从哪里获得的?在我看来像Java

 WebElement = driver.findElement(By.xpath("//div[@class='main']"));

webElement.getText();
尝试:

导入硒元素
从selenium导入webdriver
driver=webdriver.Firefox()
司机,上车http://www.helloworld.com/')
元素=驱动程序。按元素id('main')查找元素
print element.text#div main下没有文本,您希望看到什么?
footer=驱动程序。按\u id(“footer”)查找\u元素\u
打印页脚.text
#应打印“版权所有©2013 helloworld.com。保留所有权利。关于我们|隐私政策”

以下内容是从哪里获得的?在我看来像Java

 WebElement = driver.findElement(By.xpath("//div[@class='main']"));

webElement.getText();
尝试:

导入硒元素
从selenium导入webdriver
driver=webdriver.Firefox()
司机,上车http://www.helloworld.com/')
元素=驱动程序。按元素id('main')查找元素
print element.text#div main下没有文本,您希望看到什么?
footer=驱动程序。按\u id(“footer”)查找\u元素\u
打印页脚.text
#应打印“版权所有©2013 helloworld.com。保留所有权利。关于我们|隐私政策”

如果所需信息位于id为
botloc
的div中,则需要获取该元素

import selenium 
from selenium import webdriver

driver = webdriver.Firefox() 
driver.get('http://www.helloworld.com/')
bot_location = driver.find_element_by_id('botloc').text
print bot_location

如果所需信息位于id为
botloc
的div中,则需要获取该元素

import selenium 
from selenium import webdriver

driver = webdriver.Firefox() 
driver.get('http://www.helloworld.com/')
bot_location = driver.find_element_by_id('botloc').text
print bot_location

好的,看起来它可以工作,但是为什么当我在Shell中使用命令
main\u text
时,响应总是
u'
?这不是来自div的文本。你知道我哪里做错了吗?打印元素
的结果是什么。来自shell的text
吗?@Mezulith:因为
div main
中没有文本。你期望的文本是什么?因为它是唯一的示例站点,真正的站点是这是浏览器中的游戏,当你登录后,有一个带有文本的div,该文本是方形地图上的一条线,我需要它来创建我自己的bot。我是python新手,但我想根据自己的知识实现该项目。遗憾的是,“print element.text”的结果是一样的,我花了10个小时试图用div从正方形地图上获取线。我使用示例站点和示例div只是出于谨慎。如果打印
driver.page\u source
,会发生什么?在你登录之后?这是您希望看到的页面吗?嗯,看起来很有效,但为什么在Shell中使用命令
main\u text
时,响应总是
u'
?这不是来自div的文本。你知道我哪里做错了吗?打印元素
的结果是什么。来自shell的text
吗?@Mezulith:因为
div main
中没有文本。你期望的文本是什么?因为它是唯一的示例站点,真正的站点是这是浏览器中的游戏,当你登录后,有一个带有文本的div,该文本是方形地图上的一条线,我需要它来创建我自己的bot。我是python新手,但我想根据自己的知识实现该项目。遗憾的是,“print element.text”的结果是一样的,我花了10个小时试图用div从正方形地图上获取线。我使用示例站点和示例div只是出于谨慎。如果打印
driver.page\u source
,会发生什么?在你登录之后?这是你期待的页面吗?你是最棒的!答案来自师父,他让我继续,而不是让我被困在那里两天,我感谢你soooo muuuch!!在下一步中,我将尝试循环bot_位置,以每秒钟刷新一次。我构造了:
import selenium from selenium import webdriver driver=webdriver.Firefox()driver.get('http://game6.margonem.pl//“)botloc=driver.find_element_by_id('botloc')。文本打印botloc def repeat():而1==1:botloc=driver.find_element_by_id('botloc')).text print botloc
但在Shell中键入repeat后的响应是:
有什么想法要解决吗?您是否只是否决投票,拒绝接受我的答案,然后在评论中提出第二个问题?哇!您需要调用
repeat()
。你可能也应该将驱动程序传递到函数中。你是最棒的!答案来自师父,他让我继续,而不是让我被困在那里两天,我感谢你soooo muuuch!!在下一步中,我将尝试循环bot_位置,以每秒钟刷新一次。我构造了:
import selenium from selenium import webdriver driver=webdriver.Firefox()driver.get('http://game6.margonem.pl//“)botloc=driver.find_element_by_id('botloc')。文本打印botloc def repeat():而1==1:botloc=driver.find_element_by_id('botloc')).text print botloc
但在Shell中键入repeat后的响应是:
任何id