Python Selenium.text求解数学公式

2024-10-06 07:08:50 发布

您现在位置:Python中文网/ 问答频道 /正文

在该站点中,有一个特定类包含一个数学公式,如:

7-6=

15 x 8=

3 x(5-2)=

<div tabindex="20" dir="ltr" class="card-title">3 x (5 - 2) =</div>

如何导出硒元素含量并对其进行溶解

我的代码:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
PATH = "H:/python/venv/chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options, executable_path=r'H:/python/venv/chromedriver.exe')



driver.get("https://freerice.com/categories/basic-math-pre-algebra")

driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/div[1]/button").click()
driver.implicitly_wait(10)
feladat = str(driver.find_element_by_xpath("/html/body/div[2]/section/div/div[1]/div/div[2]/div[4]/div[1]/div/div/div/div/div/div[1]").text)
megoldas = [feladat]
lista = []
for c in feladat:
    if c.isdigit():
        lista = lista + [c]
    else:
        lista = lista + [c]
print(lista)

Tags: fromimportdivaddvenvdriverseleniumexe
1条回答
网友
1楼 · 发布于 2024-10-06 07:08:50

我知道你的解决方案是什么了-把所有的字符分开,识别数字,然后(我想)你会找到所有的数学运算符

有一个更简单的方法

evalexec函数接收字符串并计算输出。只要该字符串有效,它就会工作

我使用了eval作为我第一次键入它,它工作了。如果你需要了解更多,谷歌搜索这两个

根据您的输入:

7 - 6 =
15 x 8 =
3 x (5 - 2) =

典型的语法是x = 1 + 1。 我们需要:

  • 移除末端的“=”a
  • 我们需要在开始时添加一个返回变量

我们的做法如下:

#Your string - the most complex one you provided
inputText = "3 x (5-2) = "

#remove the = by splitting on it and then essentially taking everything to the left
inputText = inputText.split("=")[0]

# x is a character, * is the operator   - Any other data might need processing here
inputText = inputText.replace("x", "*")

#Create an empty variable called "answer"
answer= ""

#build a string line "myVariable = 1 + 1"
appendText = "answer = " + inputText

#execute the string. 
exec(appendText)

#output the answer
print (answer)

输出为:

9

查看您的站点,并使其与您当前的方法保持一致,我将按如下方式更新它:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument(" disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)
driver.get("https://freerice.com/categories/basic-math-pre-algebra")

#OK cookies button
OKButton = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-qa='oil-YesButton']")))
OKButton.click()

inputText = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='card-title']")))
inputText = inputText.text.split("=")[0]
inputText = inputText.replace("x", "*")
answer = ""
appendText = "answer = "+ inputText
exec(appendText)

answer = int(answer)  #remove any added float when doing division
print (answer)

#find the div that has the answer and click it
driver.find_element_by_xpath("//div[text()='" + str(answer) + "']").click()

我还更新了标识符以便更容易理解

相关问题 更多 >