I want to click this button on the Pubchem

Viewed 62

enter image description here

As you can see, when I input a word like 'acid value' in the Python, the Python starts click 'Chemical and Physical Properties'- > 'Experimental Properties' -> ... nodes automatically in the Python.

The link is here https://pubchem.ncbi.nlm.nih.gov/classification/#hid=72

Here is my code but I can`t go on anymore.

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()

Next step is click 'acid value'.

1 Answers

Try looking by text:

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()

Using long unstable locators is the main problem in your code. To pass any text as a variable do the following:

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
Related