from selenium import webdriver
from selenium import webdriver
chrome_path=r"G:\My Drive\chrome_driver\chromedriver_win32\chromedriver.exe"
driver=webdriver.Chrome(chrome_path)
driver.get('https://meshb.nlm.nih.gov/treeView')
sidebar = driver.find_element_by_xpath("/html/body/div[2]/div")
i=1
for i in range(16): # since I have 16 div(s)
sidebar.find_elements_by_xpath("/html/body/div[2]/div/div[i]")
element = driver.find_element_by_xpath("/html/body/div[2]/div").find_element_by_xpath("/html/body/div[2]/div/div[i]").find_element_by_xpath("//*[@class='ng-scope']/span")
element.click()
But I keep getting this error:
no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div/div[i]"}
I updated my code with this line: element = driver.find_element_by_xpath("/html/body/div[2]/div").find_element_by_xpath("/html/body/div[2]/div/div[" + str(i) + "]").find_element_by_xpath("/html/body/div[2]/div/div[" + str(i) + "]/div/span")When I print it, it shows me the first level nodes like Anatomy,.... But it does not open all plus buttons!
while True:
elements = driver.find_elements_by_xpath("//a[@class='ng-scope']/following-sibling::span[@ng-if = 'node.HasChildren']/i")
if (len(elements) > 0):
elements[0].click() # click on the first in the list
driver.implicitly_wait(10) # wait until list will be updated
continue
break
spans = driver.find_elements_by_xpath("//span[contains(., 'Cardiovascular')]/parent::*/parent::*/descendant-or-self::node()/a/span")
for span in spans:
print (span.text)
from selenium import webdriverfrom selenium import webdriver
from selenium.webdriver.common.by import Byimport time
chrome_path=r"E:\Selenium\drivers\Chrome64\chromedriver.exe"driver=webdriver.Chrome(chrome_path)
#navigate to websitedriver.get('https://meshb.nlm.nih.gov/treeView')
# Get list of nodesmain_list = driver.find_elements_by_xpath("//div[@ng-repeat='node in trees']/div/span")
# Iterate for list of nodesfor i in range(1, len(main_list)+1):
# Expand each node node_expand_icon = driver.find_element_by_xpath("(//div[@ng-repeat='node in trees']/div/span)[" + str(i) + "]") node_expand_icon.click(); # Wait for 5 seconds time.sleep(5)
# Get list of sub-nodes sub_nodes_list=driver.find_elements_by_xpath("(//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li") # Iterate for list of sub-nodes for j in range(1, len(sub_nodes_list)+1): sub_node_text=driver.find_element_by_xpath("((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "]").text
# If sub-node contains "Cardiovascular" then expand if "Cardiovascular" in sub_node_text: sub_node_expand_icon=driver.find_element_by_xpath("((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li/span)[" + str(j) + "]") sub_node_expand_icon.click(); # Wait for 5 seconds time.sleep(5)
# Get sub-sub-node lists sub_sub_nodes_list=driver.find_elements_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li")
# Iterate for list of sub-sub-nodes for k in range(1, len(sub_sub_nodes_list)+1):
sub_sub_node_text=driver.find_element_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]").text print sub_sub_node_text
from selenium import webdriverfrom selenium import webdriverfrom selenium.webdriver.common.by import Byimport time
chrome_path=r"E:\Selenium\drivers\Chrome64\chromedriver.exe"driver=webdriver.Chrome(chrome_path)
#navigate to websitedriver.get('https://meshb.nlm.nih.gov/treeView')
# Get list of nodesmain_list = driver.find_elements_by_xpath("//div[@ng-repeat='node in trees']/div/span")
# Iterate for list of nodesfor i in range(1, len(main_list)+1):
# Expand each node node_expand_icon = driver.find_element_by_xpath("(//div[@ng-repeat='node in trees']/div/span)[" + str(i) + "]") node_expand_icon.click(); # Wait for 5 seconds
time.sleep(2)
# Get list of sub-nodes sub_nodes_list=driver.find_elements_by_xpath("(//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li") # Iterate for list of sub-nodes for j in range(1, len(sub_nodes_list)+1): sub_node_text=driver.find_element_by_xpath("((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "]").text
# If sub-node contains "Cardiovascular" then expand if "Cardiovascular" in sub_node_text: sub_node_expand_icon=driver.find_element_by_xpath("((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li/span)[" + str(j) + "]") sub_node_expand_icon.click(); # Wait for 5 seconds
time.sleep(2)
# Get sub-sub-node lists sub_sub_nodes_list=driver.find_elements_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li")
# Iterate for list of sub-sub-nodes for k in range(1, len(sub_sub_nodes_list)+1):
child_sub_node=driver.find_element_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]")
# Expand if it has child element's try: has_child_element = driver.find_element_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]/span") has_child_element.click();
# Wait for 5 seconds
time.sleep(2) print child_sub_node.text # Get Super Sub node super_sub_node_list=driver.find_elements_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]/ul/li")
# Iterate for list of sub-sub-nodes for l in range(1, len(super_sub_node_list)+1):
super_child_sub_node=driver.find_element_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]/ul/li[" + str(l) + "]")
# Expand super childs if try: has_super_child_element = driver.find_element_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]/ul/li[" + str(l) + "]/span") has_super_child_element.click();
# Wait for 5 seconds
time.sleep(2)
print super_child_sub_node.text
super_super_sub_node=driver.find_elements_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]/ul/li")
# Iterate for list of sub-sub-nodes for m in range(1, len(super_super_sub_node)+1):
super_super_child_sub_node=driver.find_element_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]/ul/li[" + str(l) + "]/ul/li[" + str(m) + "]")
# Expand super childs if try: has_super_child_element = driver.find_element_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]/ul/li[" + str(l) + "]/ul/li[" + str(m) + "]/span") has_super_child_element.click();
# Wait for 5 seconds
time.sleep(2)
print super_super_child_sub_node.text
a_list=driver.find_element_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]/ul/li[" + str(l) + "]/ul/li[" + str(m) + "]/ul/li")
# Iterate for list of sub-sub-nodes for n in range(1, len(a_list)+1):
super_super_super_child_sub_node=driver.find_element_by_xpath("(((//div[@ng-repeat='node in trees']/div)[" + str(i) + "]/ul/li)[" + str(j) + "])/ul/li[" + str(k) + "]/ul/li[" + str(l) + "]/ul/li[" + str(m) + "]/ul/li[" + str(n) + "]") print super_super_super_child_sub_node.text except Exception: print super_super_child_sub_node.text
except Exception: print super_child_sub_node.text
except Exception: #print Exception print child_sub_node.text
#print child_sub_node.text