首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想单击Pubchem上的此按钮

我想单击Pubchem上的此按钮
EN

Stack Overflow用户
提问于 2021-04-20 23:02:34
回答 1查看 50关注 0票数 2

enter image description here

正如你所看到的,当我在Python中输入一个像'acid value‘这样的单词时,Python就会开始点击’化学和物理属性‘->’实验属性‘-> ...Python中的自动节点。

链接在这里https://pubchem.ncbi.nlm.nih.gov/classification/#hid=72

这是我的代码,但我不能再继续下去了。

代码语言:javascript
复制
from selenium import webdriver
import time
from selenium.webdriver.common.by import By


driver = webdriver.Chrome(chromedriver.exe')
driver.get("https://pubchem.ncbi.nlm.nih.gov/classification/#hid=72")
driver.implicitly_wait(5) 
driver.find_element_by_xpath("""/html/body/div/div/div[1]/div[2]/div[4]/ul/li/ul/li[6]/span[1]""").click()
driver.implicitly_wait(5) 
driver.find_element_by_xpath("""/html/body/div/div/div[1]/div[2]/div[4]/ul/li/ul/li[6]/ul/li[1]/span[1]""").click()

下一步是点击“acid值”。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-20 23:46:43

尝试通过文本查找:

代码语言:javascript
复制
from selenium import webdriver

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.implicitly_wait(10)
driver.get("https://pubchem.ncbi.nlm.nih.gov/classification/#hid=72")
assert "PubChem Classification Browser" in driver.title
driver.find_element_by_xpath("//span[contains(text(), 'Chemical and Physical Properties')]").click()
driver.find_element_by_xpath("//span[contains(text(), 'Experimental Properties')]").click()
field_value = driver.find_element_by_xpath("//span[contains(text(), 'Kovats Retention Index')]/parent::li/descendant::span[contains(@class, 'ui-button-text')][2]").text
print("Kovats Retention Index value " + field_value)
driver.close()
driver.quit()

使用长的不稳定的定位器是你代码中的主要问题。要将任何文本作为变量传递,请执行以下操作:

代码语言:javascript
复制
kovats = "Kovats Retention Index"
field_value = driver.find_element_by_xpath(f"//span[contains(text(), '{kovats}')]/parent::li/descendant::span[contains(@class, 'ui-button-text')][2]").text
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67181500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档