Python 如何使用SeleniumWebDriver findElement(By.xpath())?

Python 如何使用SeleniumWebDriver findElement(By.xpath())?,python,selenium-webdriver,phantomjs,Python,Selenium Webdriver,Phantomjs,请告诉我,我正试图从巴西国会的一个公共网站上获取信息:投票会的名称、日期和投票表 本网站: 我使用了Python 3、selenium webdriver和PhantomJS: from selenium import webdriver path_to_phantomjs = '/Users/George/Documents/phantomjs/phantomjs-2.1.1-windows/bin/phantomjs' browser = webdriver.PhantomJS(exec

请告诉我,我正试图从巴西国会的一个公共网站上获取信息:投票会的名称、日期和投票表

本网站:

我使用了Python 3、selenium webdriver和PhantomJS:

from selenium import webdriver

path_to_phantomjs = '/Users/George/Documents/phantomjs/phantomjs-2.1.1-windows/bin/phantomjs'

browser = webdriver.PhantomJS(executable_path = path_to_phantomjs)

browser.get("http://www2.camara.leg.br/atividade-legislativa/plenario/chamadaExterna.html?link=http://www.camara.gov.br/internet/votacao/mostraVotacao.asp?ideVotacao=6706&tipo=partido")

nome_votacao = browser.find_element_by_xpath("//[@id='corpoVotacao']/p[3]/text()")

data_votacao = browser.find_element_by_xpath("//[@id='corpoVotacao']/div[1]/div[1]/div/div/p[1]/text()[1]")

list_deputados = browser.find_elements_by_xpath(".//table[@class='tabela-2']")
但看起来我选错了位置

nome_votacao显示以下错误消息:

---------------------------------------------------------------------------
InvalidSelectorException                  Traceback (most recent call last)
<ipython-input-11-e67933637ae0> in <module>()
----> 1 nome_votacao = browser.find_element_by_xpath("//[@id='corpoVotacao']/p[3]/text()")

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_xpath(self, xpath)
    363             driver.find_element_by_xpath('//div/td[1]')
    364         """
--> 365         return self.find_element(by=By.XPATH, value=xpath)
    366 
    367     def find_elements_by_xpath(self, xpath):

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
    841         return self.execute(Command.FIND_ELEMENT, {
    842             'using': by,
--> 843             'value': value})['value']
    844 
    845     def find_elements(self, by=By.ID, value=None):

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    306         response = self.command_executor.execute(driver_command, params)
    307         if response:
--> 308             self.error_handler.check_response(response)
    309             response['value'] = self._unwrap_value(
    310                 response.get('value', None))

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    192         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    193             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 194         raise exception_class(message, screen, stacktrace)
    195 
    196     def _value_or_default(self, obj, key, default):

InvalidSelectorException: Message: {"errorMessage":"Unable to locate an element with the xpath expression //[@id='corpoVotacao']/p[3]/text() because of the following error:\nError: INVALID_EXPRESSION_ERR: DOM XPath Exception 51","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"118","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:55799","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"value\": \"//[@id='corpoVotacao']/p[3]/text()\", \"sessionId\": \"366665f0-be24-11e7-aa25-75da268b98e2\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/366665f0-be24-11e7-aa25-75da268b98e2/element"}}
Screenshot: available via screen
---------------------------------------------------------------------------
InvalidSelectorException                  Traceback (most recent call last)
<ipython-input-12-24c43341f310> in <module>()
----> 1 data_votacao = browser.find_element_by_xpath("//[@id='corpoVotacao']/div[1]/div[1]/div/div/p[1]/text()[1]")

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_xpath(self, xpath)
    363             driver.find_element_by_xpath('//div/td[1]')
    364         """
--> 365         return self.find_element(by=By.XPATH, value=xpath)
    366 
    367     def find_elements_by_xpath(self, xpath):

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
    841         return self.execute(Command.FIND_ELEMENT, {
    842             'using': by,
--> 843             'value': value})['value']
    844 
    845     def find_elements(self, by=By.ID, value=None):

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    306         response = self.command_executor.execute(driver_command, params)
    307         if response:
--> 308             self.error_handler.check_response(response)
    309             response['value'] = self._unwrap_value(
    310                 response.get('value', None))

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    192         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    193             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 194         raise exception_class(message, screen, stacktrace)
    195 
    196     def _value_or_default(self, obj, key, default):

InvalidSelectorException: Message: {"errorMessage":"Unable to locate an element with the xpath expression //[@id='corpoVotacao']/div[1]/div[1]/div/div/p[1]/text()[1] because of the following error:\nError: INVALID_EXPRESSION_ERR: DOM XPath Exception 51","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"143","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:55799","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"value\": \"//[@id='corpoVotacao']/div[1]/div[1]/div/div/p[1]/text()[1]\", \"sessionId\": \"366665f0-be24-11e7-aa25-75da268b98e2\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/366665f0-be24-11e7-aa25-75da268b98e2/element"}}
Screenshot: available via screen
---------------------------------------------------------------------------
InvalidSelectorException回溯(最后一次最近调用)
在()
---->1 nome_votacao=browser。通过xpath(“/[@id='corpoVotacao']]/p[3]/text()”)查找元素
c:\users\george\appdata\local\programs\python36-32\code\votos\lib\site packages\selenium\webdriver\remote\webdriver.py in find\u element\u by\u xpath(self,xpath)
363驱动程序。通过xpath(“//div/td[1]”查找元素
364         """
-->365返回self.find_元素(by=by.XPATH,value=XPATH)
366
367 def通过xpath查找元素(self,xpath):
c:\users\george\appdata\local\programs\python36-32\code\votos\lib\site packages\selenium\webdriver\remote\webdriver.py在find_元素中(self、by、value)
841返回self.execute(Command.FIND_元素{
842“使用”:通过,
-->843“值”:值})[“值”]
844
845 def find_元素(self,by=by.ID,value=None):
c:\users\george\appdata\local\programs\python36-32\code\votos\lib\site packages\selenium\webdriver\remote\webdriver.py in execute(self,driver\u命令,参数)
306响应=self.command\u executor.execute(driver\u command,params)
307如果答复:
-->308自我错误处理程序检查响应(响应)
309响应['value']=self.\u展开值(
310响应。获取('值',无))
c:\users\george\appdata\local\programs\python36-32\code\votos\lib\site packages\selenium\webdriver\remote\errorhandler.py in check\u response(self,response)
192 elif exception_class==值中的UnexpectedAlertPresentException和“alert”:
193引发异常_类(消息、屏幕、堆栈跟踪、值['alert'].get('text'))
-->194升起异常_类(消息、屏幕、堆栈跟踪)
195
196定义值或默认值(self、obj、key、default):
InvalidSelectorException:Message:{“errorMessage”:“由于以下错误,无法找到xpath表达式为//[@id='corpoVotacao']/p[3]/text()的元素:\n错误:无效表达式\u ERR:DOM xpath异常51”,“请求”:{“标头”:{“接受”:“应用程序/json”,“接受编码”:“标识”,“连接”:“关闭”,“内容长度”:”118,“内容类型”:“应用程序/json;字符集=UTF-8”,“主机”:“127.0.0.1:55799”,“用户代理”:“Python http auth”},“httpVersion”:“1.1”,“方法”:“POST”,“POST”:“{\”使用\“:\”xpath\”,“值\“:\”/[@id='corpoVotacao']]/p[3]/text()\”,“sessionId\:\:“366665f0-be24-11e7-aa25-75da268b98e2\”,“url/”元素“,“{”解析的url:“URLD”,“查询文件”:“:”元素、目录、路径、元素、相对、元素、端口、主机、密码、用户、用户信息、权限、协议、源、元素、查询、块、会话、366665f0-be24-11e7-aa25-75da268b98e2/element
屏幕截图:可通过屏幕
数据_votacao显示此错误消息:

---------------------------------------------------------------------------
InvalidSelectorException                  Traceback (most recent call last)
<ipython-input-11-e67933637ae0> in <module>()
----> 1 nome_votacao = browser.find_element_by_xpath("//[@id='corpoVotacao']/p[3]/text()")

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_xpath(self, xpath)
    363             driver.find_element_by_xpath('//div/td[1]')
    364         """
--> 365         return self.find_element(by=By.XPATH, value=xpath)
    366 
    367     def find_elements_by_xpath(self, xpath):

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
    841         return self.execute(Command.FIND_ELEMENT, {
    842             'using': by,
--> 843             'value': value})['value']
    844 
    845     def find_elements(self, by=By.ID, value=None):

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    306         response = self.command_executor.execute(driver_command, params)
    307         if response:
--> 308             self.error_handler.check_response(response)
    309             response['value'] = self._unwrap_value(
    310                 response.get('value', None))

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    192         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    193             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 194         raise exception_class(message, screen, stacktrace)
    195 
    196     def _value_or_default(self, obj, key, default):

InvalidSelectorException: Message: {"errorMessage":"Unable to locate an element with the xpath expression //[@id='corpoVotacao']/p[3]/text() because of the following error:\nError: INVALID_EXPRESSION_ERR: DOM XPath Exception 51","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"118","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:55799","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"value\": \"//[@id='corpoVotacao']/p[3]/text()\", \"sessionId\": \"366665f0-be24-11e7-aa25-75da268b98e2\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/366665f0-be24-11e7-aa25-75da268b98e2/element"}}
Screenshot: available via screen
---------------------------------------------------------------------------
InvalidSelectorException                  Traceback (most recent call last)
<ipython-input-12-24c43341f310> in <module>()
----> 1 data_votacao = browser.find_element_by_xpath("//[@id='corpoVotacao']/div[1]/div[1]/div/div/p[1]/text()[1]")

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_xpath(self, xpath)
    363             driver.find_element_by_xpath('//div/td[1]')
    364         """
--> 365         return self.find_element(by=By.XPATH, value=xpath)
    366 
    367     def find_elements_by_xpath(self, xpath):

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
    841         return self.execute(Command.FIND_ELEMENT, {
    842             'using': by,
--> 843             'value': value})['value']
    844 
    845     def find_elements(self, by=By.ID, value=None):

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    306         response = self.command_executor.execute(driver_command, params)
    307         if response:
--> 308             self.error_handler.check_response(response)
    309             response['value'] = self._unwrap_value(
    310                 response.get('value', None))

c:\users\george\appdata\local\programs\python\python36-32\code\votos\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    192         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    193             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 194         raise exception_class(message, screen, stacktrace)
    195 
    196     def _value_or_default(self, obj, key, default):

InvalidSelectorException: Message: {"errorMessage":"Unable to locate an element with the xpath expression //[@id='corpoVotacao']/div[1]/div[1]/div/div/p[1]/text()[1] because of the following error:\nError: INVALID_EXPRESSION_ERR: DOM XPath Exception 51","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"143","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:55799","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"value\": \"//[@id='corpoVotacao']/div[1]/div[1]/div/div/p[1]/text()[1]\", \"sessionId\": \"366665f0-be24-11e7-aa25-75da268b98e2\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/366665f0-be24-11e7-aa25-75da268b98e2/element"}}
Screenshot: available via screen
---------------------------------------------------------------------------
InvalidSelectorException回溯(最后一次最近调用)
在()
---->1 data\u votacao=browser。通过xpath(“/[@id='corpoVotacao']/div[1]/div[1]/div/div/p[1]/text()[1]”查找元素
c:\users\george\appdata\local\programs\python36-32\code\votos\lib\site packages\selenium\webdriver\remote\webdriver.py in find\u element\u by\u xpath(self,xpath)
363驱动程序。通过xpath(“//div/td[1]”查找元素
364         """
-->365返回self.find_元素(by=by.XPATH,value=XPATH)
366
367 def通过xpath查找元素(self,xpath):
c:\users\george\appdata\local\programs\python36-32\code\votos\lib\site packages\selenium\webdriver\remote\webdriver.py在find_元素中(self、by、value)
841返回self.execute(Command.FIND_元素{
842“使用”:通过,
-->843“值”:值})[“值”]
844
845 def find_元素(self,by=by.ID,value=None):
c:\users\george\appdata\local\programs\python36-32\code\votos\lib\site packages\selenium\webdriver\remote\webdriver.py in execute(self,driver\u命令,参数)
306响应=self.command\u executor.execute(driver\u command,params)
307如果答复:
-->308自我错误处理程序检查响应(响应)
309响应['value']=self.\u展开值(
310响应。获取('值',无))
c:\users\george\appdata\local\programs\python36-32\code\votos\lib\site packages\selenium\webdriver\remote\errorhandler.py in check\u response(self,response)
192 elif exception_class==值中的UnexpectedAlertPresentException和“alert”:
193引发异常_类(消息、屏幕、堆栈跟踪、值['alert'].get('text'))
-->194升起异常_类(消息、屏幕、堆栈跟踪)
195
196定义值或默认值(self、obj、key、default):
InvalidSelectorException:Message:{“errorMessage”:“无法找到xpath表达式为/[@id='corpoVotacao']/div[1]/div[1]/div/div/p[1]/text()[1]的元素,因为出现以下错误:\n错误:无效的表达式\u ERR:DOM xpath异常51”,“请求”:{“头”:{“接受”:“应用程序/json”,“接受编码”:“标识”,“连接”:“关闭”,“内容长度”:“143”,“内容类型”:“应用程序/json;字符集=UTF-8”,“主机”:“127.0.0.1:55799”,“用户代理”:“Python http auth”},“httpVersion”:“1.1”,“方法”:“POST”,“POST”:“{\”使用\“:\”xpath\”,\“值\:\”/[@id='corpoVotacao']/div[1]/div/div/p[1]/text()”
driver = webdriver.PhantomJS(executable_path=r'/pathTo/phantomjs')
driver.get("http://www2.camara.leg.br/atividade-legislativa/plenario/chamadaExterna.html?link=http://www.camara.gov.br/internet/votacao/mostraVotacao.asp?ideVotacao=6706&tipo=partido")
iframe = driver.find_element_by_xpath("//iframe[@id='ifr']")
driver.switch_to_frame(iframe)

nome_votacao = driver.find_element_by_xpath("//*[@id='corpoVotacao']/p[3]")
data_votacao = driver.find_element_by_xpath("//*[@id='corpoVotacao']/div[1]/div[1]/div/div/p[1]")
list_deputados = driver.find_elements_by_xpath("*//div[@id='listagem']/table[@class='tabela-2']/tbody/tr/td")

print(nome_votacao.text)
print("----------------------------")

print(data_votacao.text)

print("----------------------------")


i=0
maxLen=len(list_deputados)
print("maxLen: " + str(maxLen))
for deputado in list_deputados:
    print("Parlamentar DEM: " + list_deputados[i].text)
    print("UF: " + list_deputados[i+1].text)
    print("Voto: " + list_deputados[i+2].text)
    i=i+2
    print("--------------"+str(i)+"-------------")
    if(i==maxLen-3): #don't consider the last row: Total Solidaried: 11
        break


driver.close()