Reputation: 13
I want to get some information from a website and Chrome should run in the background to fulfill that task. Down there you can see my code. It works so far and I get the desired output, but when I add the chrome_options
so that Chrome is hidden I don't get the output anymore.
What's the problem and how can I fix this?
from selenium import webdriver
def get_stockname(wkn):
PATH = r"***placeholder***chromedriver.exe"
url = "***placeholder***"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("headless")
browser = webdriver.Chrome(PATH, options=chrome_options)
browser.get(url)
search_box = browser.find_element_by_class_name('input-field__text-input')
search_box.send_keys(wkn)
search_box.submit()
name = browser.find_element_by_xpath("/html/body/div[2]/div[1]/div[2]/div[13]/div[2]/div[1]/h2").text
name = name[13:]
print(name)
Upvotes: 1
Views: 97
Reputation: 3537
try like that, but with replacing your url:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
url = "https://google.com"
chrome_options = Options()
chrome_options.add_argument("--headless")
browser = webdriver.Chrome(options=chrome_options)
browser.get(url)
search_box = browser.find_element_by_class_name('input-field__text-input')
search_box.send_keys(wkn)
search_box.submit()
name = browser.find_element_by_xpath("(//h2[@class='box-headline'])[2]").get_attribute('innerText')
name = name[13:]
print(name)
By the way, can you show DOM code snipped to make element more unique with such external relation or even URL and element what info you want to get out?
Upvotes: 1