Unlike local, you need to run chrome in a headless browser.
-Process on the back side without launching a visible browser. ・ In the case of chrome, it is called headless chrome.
python
from selenium import webdriver
#Import Options class (for headless settings)
from selenium.webdriver.chrome.options import Options
#Create an instance of Options (stored in the variable options)
options = Options()
#Set headless to True
options.headless = True
#Start webdriver
driver = webdriver.Chrome(options=options)
#Open the specified URL
driver.get("URL")
#Describe the process to be executed below
processing
The headless browser can be launched and the process can be executed.
・ Selenium.webdriver.chrome.options.Options
└ Options class location
└ Options class in options module in chrome module in webdriver module in selenium module
・ From selenium.webdriver.chrome.options import Options
└ Import Options class
└ Call after import is possible with Options
Reference link ・ Explanation of Options-Official Site About importing classes
It is interesting because you can see the contents of the method on the above official website.
options.headless = True
-Set the value of the argument "headless" to True.
-The headless method itself returns whether headless is set as an argument.
You can also set headless using the ** add_argument method **.
options.add_argument('--headless')
└ Set "'--headless'" with the argument.
webdriver.Chrome(options=options) -A function that starts the webdriver. -Set startup options with arguments. -Default options = none is set for the created instance. (I'm not confident, please let me know if anyone is familiar with it)
python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get("https://www.google.co.jp/")
element_text = driver.find_element_by_id("hptl").text
print(element_text)
"About Google Store" is displayed.
driver.get('url')
** ▼ Difference from headless **
-There is no headless setting.
-The argument when starting webdriver is chromedriver.exe
-"Chromedriver.exe" must be in the same hierarchy.
└ In case of another layer, specify the path as an argument.
<br>
There is a difference from the execution method in the local environment, but you can use it without problems as long as you make the initial settings.
Recommended Posts