Selenium 使用xpath选择元素
硒元素在这里非常新,但是我在从这个网站上选择我想要的元素时遇到了困难。在本例中,我使用Chrome的“复制XPath工具”获得了x_路径。基本上,我希望从网站中提取CID文本(在本例中为4004),但我的代码似乎无法做到这一点。任何帮助都将不胜感激 我也尝试过使用CSS选择器方法,但它返回相同的错误Selenium 使用xpath选择元素,selenium,Selenium,硒元素在这里非常新,但是我在从这个网站上选择我想要的元素时遇到了困难。在本例中,我使用Chrome的“复制XPath工具”获得了x_路径。基本上,我希望从网站中提取CID文本(在本例中为4004),但我的代码似乎无法做到这一点。任何帮助都将不胜感激 我也尝试过使用CSS选择器方法,但它返回相同的错误 chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.binary_loca
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
driver= webdriver.Chrome()
chem_name = "D008294"
url = "https://pubchem.ncbi.nlm.nih.gov/#query=" + chem_name
driver.get(url)
elements = driver.find_elements_by_xpath('//*[@id="collection-results-container"]/div/div/div[2]/ul/li/div/div/div/div[2]/div[2]/div[2]/span/a/span/span')
driver.close()
print(elements.text)
到目前为止,这是我收到的错误:“list”对象没有属性“text”这是您可以使用的xpath
//span[.='Compound CID']//following-sibling::a/descendant::span[2]
脚本不起作用的原因:代码中存在2个问题
elements = driver.find_elements_by_xpath('//*[@id="collection-results-container"]/div/div/div[2]/ul/li/div/div/div/div[2]/div[2]/div[2]/span/a/span/span')
driver.close() # <== don't close the browser until you are done with all your steps on the browser or elements
print(elements.text) # <== you can not get text from list (python will through error here
elements=driver。通过xpath('/*[@id=“collection results container”]/div/div/div[2]/ul/li/div/div/div[2]/div[2]/div[2]/span/a/span/span'查找元素
driver.close()#这是您可以使用的xpath
//span[.='Compound CID']//following-sibling::a/descendant::span[2]
脚本不起作用的原因:代码中存在2个问题
elements = driver.find_elements_by_xpath('//*[@id="collection-results-container"]/div/div/div[2]/ul/li/div/div/div/div[2]/div[2]/div[2]/span/a/span/span')
driver.close() # <== don't close the browser until you are done with all your steps on the browser or elements
print(elements.text) # <== you can not get text from list (python will through error here
elements=driver。通过xpath('/*[@id=“collection results container”]/div/div/div[2]/ul/li/div/div/div[2]/div[2]/div[2]/span/a/span/span'查找元素
driver.close()#函数driver.find_elements_by_xpath
返回元素列表。您应该循环以获取每个元素的文本
像这样:
for ele in print(elements.text):
print(ele.text)
或者,如果要匹配第一个元素,请使用驱动程序。改为使用xpath
函数查找元素。函数驱动程序。使用xpath
返回元素列表。您应该循环以获取每个元素的文本
像这样:
for ele in print(elements.text):
print(ele.text)
或者,如果要匹配第一个元素,请使用驱动程序。改为使用xpath
函数查找\u元素。使用xpath提供的chrome is始终无法按预期工作。首先,您必须知道如何编写xpath并在chrome控制台上验证它
请参阅这些链接,它们可以帮助您了解XPath
在这种情况下,首先查找包含文本复合CID的范围,然后移动到父范围,再向下移动到子范围a/span/span。类似于//span[包含(text(),'component CID']/parent::span/a/span/span的内容
您还需要findelement,它返回单个元素并从中获取文本。如果使用findelement,它将返回元素列表,因此您需要循环并从这些元素获取文本。使用xpath提供的chrome is始终无法按预期工作。首先,您必须知道如何编写xpath并在chrome控制台中验证它
请参阅这些链接,它们可以帮助您了解XPath
在本例中,首先查找span contains text component CID,然后移动到父span,再移动到子a/span/span。类似于//span[contains(text(),'component CID']/parent::span/a/span/span
您还需要findelement返回单个元素并从中获取文本。如果使用findelement,则它将返回元素列表,因此您需要循环并从这些元素获取文本。
xpath://a[包含(@href,'component')]/span[@class='breakword']/span
您可以使用“href”作为属性引用,因为我注意到它对每个组件都有唯一的值
例如:
href=”https://pubchem.ncbi.nlm.nih.gov/物质/53790330“
href=”https://pubchem.ncbi.nlm.nih.gov/化合物/4004“
xpath://a[包含(@href,'component')]/span[@class='breakword']/span
您可以使用“href”作为属性引用,因为我注意到它对每个组件都有唯一的值
例如:
href=”https://pubchem.ncbi.nlm.nih.gov/物质/53790330“
href=”https://pubchem.ncbi.nlm.nih.gov/化合物/4004“