Can't find button by classname selenium - python

I am trying to login to https://ok.ru/
I can't find button by css selector or classname .
I want to click the button
My code returns error
Message: no such element: Unable to locate element: {"method":"css selector","selector":".button-pro __wide"}
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path='chromedriver')
def login(email, password):
driver.get(BASE_URL)
driver.find_element_by_id('field_email').send_keys(email)
driver.find_element_by_id('field_password').send_keys(password)
driver.find_element_by_class_name('button-pro __wide').click()
login(email, password)
Debugging details
<ipython-input-19-30d48e0af276> in login(email, password)
6 driver.find_element_by_id('field_email').send_keys(email)
7 driver.find_element_by_id('field_password').send_keys(password)
----> 8 driver.find_element_by_css_selector('button-pro __wide.button-pro __wide').click()
9
10 login(email, password)
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in find_element_by_css_selector(self, css_selector)
596 element = driver.find_element_by_css_selector('#foo')
597 """
--> 598 return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
599
600 def find_elements_by_css_selector(self, css_selector):
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in find_element(self, by, value)
976 return self.execute(Command.FIND_ELEMENT, {
977 'using': by,
--> 978 'value': value})['value']
979
980 def find_elements(self, by=By.ID, value=None):
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button-pro __wide.button-pro __wide"}
(Session info: chrome=85.0.4183.102)
P.S
I know that I can use xpath, but I need reliable method that uses same classname or id etc

Related

Selenium insert number into table [duplicate]

I have a page with source code like the code below. In it after I take an action an “Undo” and a “Close” button appear. I’m trying to click the “Close” button. I’ve tried all three of the chunks of code below, none are working. Can someone please point out what I’m doing wrong, or suggest something else to try?
html source:
<div class="_3Aslx7L3GVI4XM7PUyYKza action-bar"><div class="container"><i class="success-icon fontello-ok-circle"></i><div class="success-message">Your stuff is going to <span>place</span> is on its way.</div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--gray"> Undo</button></div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--blue"> Close</button></div></div></div>
code attempts:
#driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
driver.find_element_by_css_selector('.c-button.c-button--blue').click()
#driver.find_element_by_link_text('Close').click()
error:
---------------------------------------------------------------------------
ElementNotVisibleException Traceback (most recent call last)
<ipython-input-15-6d570be770d7> in <module>()
1 #driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
----> 2 driver.find_element_by_css_selector('.c-button.c-button--blue').click()
3 #driver.find_element_by_link_text('Close').click()
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in click(self)
78 def click(self):
79 """Clicks the element."""
---> 80 self._execute(Command.CLICK_ELEMENT)
81
82 def submit(self):
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
626 params = {}
627 params['id'] = self._id
--> 628 return self._parent.execute(command, params)
629
630 def find_element(self, by=By.ID, value=None):
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
318 response = self.command_executor.execute(driver_command, params)
319 if response:
--> 320 self.error_handler.check_response(response)
321 response['value'] = self._unwrap_value(
322 response.get('value', None))
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
ElementNotVisibleException: Message: element not interactable
(Session info: chrome=72.0.3626.109)
(Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)
The element with text as Close is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class, 'action-bar')]//button[#class='c-button c-button--blue' and normalize-space()='Close']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try using XPATHs and Action class for this kind of scenarios.
BTN_xpath = //*[contains(#class, 'c-button--blue')]
WebElement btn = driver.find_element_by_xpath(BTN_xpath);
Actions ac = new Actions(driver);
ac.click(btn).build().perform();
Also, if you are using the Firefox browser, there could be issues where its not able to interact with elements that were not visible before in the page. So, try checking a with a different browser as well.

WebDriverException: Message: chrome not reachable When it was working a minute before

I'm writing a scraper using Selenium, and it was working just fine until 15 minutes ago..
All of sudden, I get the following error every time I execute. This is my code:
searchDate = wait.until(EC.element_to_be_clickable((By.XPATH, "/input[#placeholder='select sold date range']")))
searchDate.click()
time.sleep(5)
And the error:
WebDriverException: Message: chrome not reachable
(Session info: chrome=80.0.3987.163)
And my chrome setting:
options = webdriver.ChromeOptions()
prefs = {'download.default_directory': new_dir}
options.add_experimental_option('prefs', prefs)
browser = webdriver.Chrome(executable_path = r'C:/Program Files/chromedriver/chromedriver.exe')
I tried to solve the problem by chain the options as other post suggested, such as:
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
prefs = {'download.default_directory': new_dir}
options.add_experimental_option('prefs', prefs)
browser = webdriver.Chrome(executable_path = r'C:/Program Files/chromedriver/chromedriver.exe', chrome_options=options)
But I still get the exact same error. Why did it happen?
Any advice will be greatly appreciated!
++
---------------------------------------------------------------------------
WebDriverException Traceback (most recent call last)
<ipython-input-102-c7d533e1df88> in <module>
----> 1 wait.until(EC.visibility_of_element_located((By.XPATH,"//input[#name='daterangepicker_start']"))).click()
2
3
~\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)
69 while True:
70 try:
---> 71 value = method(self._driver)
72 if value:
73 return value
~\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\support\expected_conditions.py in __call__(self, driver)
126 def __call__(self, driver):
127 try:
--> 128 return _element_if_visible(_find_element(driver, self.locator))
129 except StaleElementReferenceException:
130 return False
~\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\support\expected_conditions.py in _find_element(driver, by)
413 raise e
414 except WebDriverException as e:
--> 415 raise e
416
417
~\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\support\expected_conditions.py in _find_element(driver, by)
409 if thrown."""
410 try:
--> 411 return driver.find_element(*by)
412 except NoSuchElementException as e:
413 raise e
~\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
976 return self.execute(Command.FIND_ELEMENT, {
977 'using': by,
--> 978 'value': value})['value']
979
980 def find_elements(self, by=By.ID, value=None):
~\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
~\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
WebDriverException: Message: chrome not reachable
(Session info: chrome=81.0.4044.92)
Can you please check if your chrome browser is compatible with your chrome browser?
Could be possible your chrome is autommatically updated and now your chrome browser is not compatible with chrome driver.
You can download chrome driver from here

Element that could be previously clicked is now no longer clickable? [duplicate]

I have a page with source code like the code below. In it after I take an action an “Undo” and a “Close” button appear. I’m trying to click the “Close” button. I’ve tried all three of the chunks of code below, none are working. Can someone please point out what I’m doing wrong, or suggest something else to try?
html source:
<div class="_3Aslx7L3GVI4XM7PUyYKza action-bar"><div class="container"><i class="success-icon fontello-ok-circle"></i><div class="success-message">Your stuff is going to <span>place</span> is on its way.</div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--gray"> Undo</button></div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--blue"> Close</button></div></div></div>
code attempts:
#driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
driver.find_element_by_css_selector('.c-button.c-button--blue').click()
#driver.find_element_by_link_text('Close').click()
error:
---------------------------------------------------------------------------
ElementNotVisibleException Traceback (most recent call last)
<ipython-input-15-6d570be770d7> in <module>()
1 #driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
----> 2 driver.find_element_by_css_selector('.c-button.c-button--blue').click()
3 #driver.find_element_by_link_text('Close').click()
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in click(self)
78 def click(self):
79 """Clicks the element."""
---> 80 self._execute(Command.CLICK_ELEMENT)
81
82 def submit(self):
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
626 params = {}
627 params['id'] = self._id
--> 628 return self._parent.execute(command, params)
629
630 def find_element(self, by=By.ID, value=None):
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
318 response = self.command_executor.execute(driver_command, params)
319 if response:
--> 320 self.error_handler.check_response(response)
321 response['value'] = self._unwrap_value(
322 response.get('value', None))
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
ElementNotVisibleException: Message: element not interactable
(Session info: chrome=72.0.3626.109)
(Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)
The element with text as Close is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class, 'action-bar')]//button[#class='c-button c-button--blue' and normalize-space()='Close']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try using XPATHs and Action class for this kind of scenarios.
BTN_xpath = //*[contains(#class, 'c-button--blue')]
WebElement btn = driver.find_element_by_xpath(BTN_xpath);
Actions ac = new Actions(driver);
ac.click(btn).build().perform();
Also, if you are using the Firefox browser, there could be issues where its not able to interact with elements that were not visible before in the page. So, try checking a with a different browser as well.

How to click on the list of the <a> elements in an <ul> and <li> elements with selenium using python?

I'm scraping a website. I'm trying to click on a link under <li> but it throws NoSuchElementException exception.
And the links I want to click:
I'm using below code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('window-size=5000x2500')
webdriver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
url = "https://www.cofidis.es/es/creditos-prestamos/financiacion-coche.html"
webdriver.get(url)
webdriver.find_element_by_xpath('//*[#id="btncerrar"]').click()
time.sleep(5)
webdriver.find_element_by_link_text('Préstamo Coche Nuevo').click()
webdriver.save_screenshot('test1.png')
The error I got:
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:10: DeprecationWarning: use options instead of chrome_options
# Remove the CWD from sys.path while we load stuff.
---------------------------------------------------------------------------
NoSuchElementException Traceback (most recent call last)
<ipython-input-44-f6608be53ab3> in <module>()
13 webdriver.find_element_by_xpath('//*[#id="btncerrar"]').click()
14 time.sleep(5)
---> 15 webdriver.find_element_by_link_text('Préstamo Coche Nuevo').click()
16 webdriver.save_screenshot('test1.png')
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py
in find_element_by_link_text(self, link_text)
426 element = driver.find_element_by_link_text('Sign In')
427 """
--> 428 return self.find_element(by=By.LINK_TEXT, value=link_text)
429
430 def find_elements_by_link_text(self, text):
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py
in find_element(self, by, value)
976 return self.execute(Command.FIND_ELEMENT, {
977 'using': by,
--> 978 'value': value})['value']
979
980 def find_elements(self, by=By.ID, value=None):
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py
in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py
in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Préstamo Coche Nuevo"}
(Session info: headless chrome=72.0.3626.121)
(Driver info: chromedriver=72.0.3626.121,platform=Linux 4.14.79+ x86_64)
You could simply grab that url and get to it. Also, worth noting that you have a base url you can simply add the hyphenated search string to i.e. financiar-viaje on to base of https://www.cofidis.es/es/creditos-prestamos/
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://www.cofidis.es/es/creditos-prestamos/financiacion-coche.html'
driver = webdriver.Chrome()
driver.get(url)
url = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "[href*='financiar-viaje']"))).get_attribute('href')
driver.get(url)
Use below code to click on the link
webdriver.find_element_by_css_selector("#ei_tpl_navertical li>a[data='16736']").click()
Use implicit/explicit wait to make sure your element is ready to interact. in your case :
webdriver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
url = "https://www.cofidis.es/es/creditos-prestamos/financiacion-coche.html"
webdriver.get(url)
webdriver.implicitlyWait(20)
webdriver.find_element_by_id('btncerrar').click()
time.sleep(5)
webdriver.find_element_by_css_selector("#ei_tpl_navertical li>a[data='16736']").click()
webdriver.save_screenshot('test1.png')

How to use the Selenium WebDriver findElement (By.xpath ())?

please, I'm trying to get information from a public website of the Brazilian congress: name of a voting session, date and table with list of votes
This site: http://www2.camara.leg.br/atividade-legislativa/plenario/chamadaExterna.html?link=http://www.camara.gov.br/internet/votacao/mostraVotacao.asp?ideVotacao=6706&tipo=partido
I used Python 3, selenium webdriver and 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']")
But it looks like I'm wrongly selecting locations
The nome_votacao appears this error message:
---------------------------------------------------------------------------
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
The data_votacao this error message:
---------------------------------------------------------------------------
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
And the list_deputados generates an empty list
The data_votacao was to have this content: "10/11/2015 20:15". And it has this XPath in the inspect: //*[#id="corpoVotacao"]/p[2]/text()[1]
The nome_votação was to have this content: "MPV Nº 688/2015 - PROJETO DE LEI DE CONVERSÃO - Nominal Eletrônica". And it has this XPath in the inspect: //*[#id="corpoVotacao"]/p[3]/text()
And the list_deputados was to have the complete table with the votes (names, UF and vote), starting in the line "Parlamentar". It has this XPath: //*[#id="listagem"]/table. And the class="tabela-2"
Does anyone know the correct form of the commands? Or some tutorial?
The first problem is that all the interested elements are inside an iframe.
So, first you have to switch to the iframe:
iframe = driver.find_element_by_xpath("//iframe[#id='ifr']")
driver.switch_to_frame(iframe)
Also the xpath aren't correct. You could use the following:
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")
The entire code:
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()
Can you try
//*[#id="corpoVotacao"]/p[3]/text() as XPATH instead of
//[#id='corpoVotacao']/p[3]/text()
//*[#id="corpoVotacao"]/div[1]/div[1]/div/div/p[1]/text()[1] as XPATH instead of
//[#id='corpoVotacao']/div[1]/div[1]/div/div/p[1]/text()[1]
last XPATH seems correct to me, also can you have implicit wait after get() just to ensure elements are loaded on UI.

Categories

Resources