简体   繁体   中英

Is there any way to access this text using python selenium?

I want to use selenium to evaluate the accuracy of chess moves within my dataset of games for an AI. So far, I can access the evaluation bar, but not the FEN (it returns an empty string). Is there any way to access the FEN within this site .

This is the textbox: 在此处输入图像描述

I want to grab this: rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 2

This is the HTML of the textbox:

<input type="text" readonly="" class="text" id="fen_position" name="fen_position" value="" style="width:350px;font-size: 0.7em !important;padding: 3px 6px !important;">

Here is the relevant portion of my code:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

options = Options()
options.headless = True

browser = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
browser.get("https://www.365chess.com/analysis_board.php")

browser.implicitly_wait(5); fen = browser.find_element_by_id('fen_position').text
print(fen)

So the FEN is an <input> for inputs the value is stored as a "value" in properties you can use the.get_property("value") to access it. Here is the working code.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

options = Options()
options.headless = True

browser = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
browser.get("https://www.365chess.com/analysis_board.php")

browser.implicitly_wait(5); fen = browser.find_element_by_id('fen_position').get_property("value")
print(fen)

Happy coding:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM