获取特定的xelement子值

时间:2016-11-28 12:05:05

标签: c# .net xml xpath

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.chrome.service as service

ext_folder = os.environ["LOCALAPPDATA"] + "\\Google\\Chrome\\User Data\\Default\\Extensions\\blabla"

chrome_options = Options()
chrome_options.add_argument("load-extension=" + ext_folder)
chrome_path = "Path\\To\\chromedriver.exe"
service = service.Service(chrome_path)
service.start()
capabilities = {'chrome.binary': chrome_path}
browser = webdriver.Remote(service.service_url, desired_capabilities=chrome_options.to_capabilities())

browser.get('https://amazon.com')
browser.find_element(By.TAG_NAME, "body").send_keys(Keys.CONTROL + "m") # this would start the extension, but nothing happens
browser.quit()

我确实想要将元素Actions actions = new Actions(browser); actions.keyDown(Keys.CONTROL).sendKeys("m").perform(); // doesn't work actions.sendKeys(Keys.chord(Keys.ESCAPE)).perform(); // doesn't work actions.sendKeys(Keys.chord(Keys.CONTROL, "m")).perform(); // doesn't work WebElement body = browser.findElement(By.tagName("body")); body.sendKeys(Keys.chord(Keys.ESCAPE)); // doesn't work body.sendKeys(Keys.chord(Keys.CONTROL, "m")); // doesn't work Robot bot = new Robot(); bot.keyPress(KeyEvent.VK_CONTROL); bot.keyPress(KeyEvent.VK_M); bot.keyRelease(KeyEvent.VK_M); bot.keyRelease(KeyEvent.VK_CONTROL); // doesn't work 的值设为<information items ="2"> <table id="31"> </table> <profile code="5"> <name language="ro"> Spania </name> <name language="gb"> Spain </name> <name language="pl"> Hiszpania </name> </profile> </information>

我尝试过类似的事情:

<name>

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以使用XPath

使用System.Xml.Linq.XDocument

string country = xdoc.XPathSelectElement("/information/profile/name[@language='gb']").Value;

使用System.Xml.XmlDocument

string country = xdoc.SelectSingleNode("/information/profile/name[@language='gb']").InnerText;

请记住,您还需要System.Xml.XPath命名空间。