I'm doing a search on the pubchem site with the code below. I need to get the "Compound CID:" number from the screen from the search result but I couldn't get it. I need help on this.
driver = webdriver.Chrome()
url = "https://pubchem.ncbi.nlm.nih.gov/"
driver.get(url)
driver.maximize_window()
searchInput = driver.find_element_by_xpath("/html/body/div[1]/div/div/main/div[1]/div/div[2]/div/div[2]/form/div/div[1]/input")
searchInput.click()
searchInput.send_keys("75-05-8")
searchInput.send_keys(Keys.ENTER)
time.sleep(2)
driver.close()
To print the text 6342 you can use either of the following Locator Strategies:
Using css_selector and
get_attribute("innerHTML"):Using xpath and text attribute:
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and text attribute:
Using XPATH and
get_attribute("innerHTML"):Note : You have to add the following imports :
Console Output:
References
Link to useful documentation:
get_attribute()methodGets the given attribute or property of the element.textattribute returnsThe text of the element.