Use Selenium without opening browser

Asked

Viewed 510 times

0

How to use Selenium without opening the browser?

I used a code I saw right here, options.add_argument("--headless"), and also options.set_headless(True), but makes that mistake:

Selenium.common.exceptions.Nosuchelementexception: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="Contentplaceholder1_spanclosingprice"]"}

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("headless")
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://etherscan.io/tx/0xcc22fc0c43eb8c6a88ce92fd344108cf8f690c21da57a3fb0d8dadcd207d5de3")
resultado = driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_spanClosingPrice"]').text
print(resultado)
driver.quit()

What to do? I’m using Python.

Note: when I let open the browser he thinks right.

  • Could you please put the code so that we can better understand your problem? Thank you!

  • I put the code!

3 answers

1


If you don’t need to be using Selenium + Webdriver you can use the requests + Beautifulsoup:


Importing required libraries and creating header to not receive 403 error (Forbidden)

import requests
from bs4 import BeautifulSoup

cabecalho = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'}
pagina = 'https://etherscan.io/tx/0xcc22fc0c43eb8c6a88ce92fd344108cf8f690c21da57a3fb0d8dadcd207d5de3'
dados = requests.get(pagina, headers = cabecalho)

Creating the object Soup:

soup = BeautifulSoup(dados.text, 'html.parser')

Here we extract the information :

soup.find('span',{'id': 'ContentPlaceHolder1_spanClosingPrice'}).text

Upshot:

'$264.47 / ETH'

That way you don’t need to open browser.

  • thank you very much, it helped a lot

  • for nothing! Hug!!

0

Missing to import Options!

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
    
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path = r'chromedriver.exe', options = options)
driver.get("https://etherscan.io/tx/0xcc22fc0c43eb8c6a88ce92fd344108cf8f690c21da57a3fb0d8dadcd207d5de3")
resultado = driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_spanClosingPrice"]').text
print(resultado)
driver.quit()
  • Here gives the same error that she quotes above: Message: no such element: Unable to locate element:

-3

Good afternoon,

Take a look at Phantomjs https://phantomjs.org/download.html

from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550)
driver.get("url")
print(driver.current_url)
driver.quit()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.