Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C中的Selenium登录Instagram#_C#_.net_Selenium_Xpath - Fatal编程技术网

C# 使用C中的Selenium登录Instagram#

C# 使用C中的Selenium登录Instagram#,c#,.net,selenium,xpath,C#,.net,Selenium,Xpath,我正在尝试使用C#和Selenium登录Instagram,但该网站没有按钮的ID。否则我怎么做呢?我尝试过使用XPath,但对它知之甚少。这里是我迄今为止的代码示例 IWebDriver fox = new FirefoxDriver(); fox.Navigate().GoToUrl("http://www.instagram.com"); IWebElement email = fox.FindElement(By.XPath("//i[contains(@class, '_k6cv7')

我正在尝试使用C#和Selenium登录Instagram,但该网站没有按钮的ID。否则我怎么做呢?我尝试过使用XPath,但对它知之甚少。这里是我迄今为止的代码示例

IWebDriver fox = new FirefoxDriver();
fox.Navigate().GoToUrl("http://www.instagram.com");
IWebElement email = fox.FindElement(By.XPath("//i[contains(@class, '_k6cv7')]"));

按钮上的类是生成的,因此您无法使用该类找到它们

您应该做的是查看您可以找到的内容,例如“aria标签”是“username”(类似于密码),按钮上的文本是“登录”

您还可以使用chrome找到xpath(调试->右键单击元素->复制->xpath)。登录字段将为您提供如下信息

 //*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[1]/input

按钮上的类是生成的,因此您无法使用该类找到它们

您应该做的是查看您可以找到的内容,例如“aria标签”是“username”(类似于密码),按钮上的文本是“登录”

您还可以使用chrome找到xpath(调试->右键单击元素->复制->xpath)。登录字段将为您提供如下信息

 //*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[1]/input

您要做的是找到您想要的元素的独特之处,并使用它来定位它。id通常是理想的,但正如您所看到的,我们可能想要查找的许多元素都没有id,因此我们必须依赖元素的其他属性。您可以像以前一样查找类、名称或任何其他属性。我通常使用CSS选择器来查找元素。我会给你几个例子,然后在底部放几个链接,你可以在后面回顾和参考

电子邮件
输入

<input type="text" class="_kp5f7 _qy55y" aria-describedby="" aria-label="Email"
  aria-required="true" autocapitalize="off" autocorrect="off" name="email"
  placeholder="Email">
您可以通过.ClassName()使用
,但这将为您提供任何元素类型,
INPUT
p
,等等。有了这一点,您可以完成同样的任务,但要更加具体。您通常希望尽可能具体。这样做将最大限度地减少您的定位器与站点的未来更改之间的冲突

按属性

// INPUT with attribute 'name' equal to 'email'
driver.FindElement(By.CssSelector("input[name='email']"));
属性搜索的一般形式是
标记[attribute\u NAME='attribute\u VALUE']
。您可以做的远不止这些,但这是一个非常常见的CSS选择器


您要做的是找到所需元素的独特之处,并使用它来定位它。id通常是理想的,但正如您所看到的,我们可能想要查找的许多元素都没有id,因此我们必须依赖元素的其他属性。您可以像以前一样查找类、名称或任何其他属性。我通常使用CSS选择器来查找元素。我会给你几个例子,然后在底部放几个链接,你可以在后面回顾和参考

电子邮件
输入

<input type="text" class="_kp5f7 _qy55y" aria-describedby="" aria-label="Email"
  aria-required="true" autocapitalize="off" autocorrect="off" name="email"
  placeholder="Email">
您可以通过.ClassName()使用
,但这将为您提供任何元素类型,
INPUT
p
,等等。有了这一点,您可以完成同样的任务,但要更加具体。您通常希望尽可能具体。这样做将最大限度地减少您的定位器与站点的未来更改之间的冲突

按属性

// INPUT with attribute 'name' equal to 'email'
driver.FindElement(By.CssSelector("input[name='email']"));
属性搜索的一般形式是
标记[attribute\u NAME='attribute\u VALUE']
。您可以做的远不止这些,但这是一个非常常见的CSS选择器

试试这个:

IWebDriver fox = new FirefoxDriver();
fox.Navigate().GoToUrl("http://www.instagram.com");

IWebElement email = fox.FindElement(By.CssSelector("input[name='username']"));

email.Sendkeys('Kam@gmail.com');
试试这个:

IWebDriver fox = new FirefoxDriver();
fox.Navigate().GoToUrl("http://www.instagram.com");

IWebElement email = fox.FindElement(By.CssSelector("input[name='username']"));

email.Sendkeys('Kam@gmail.com');

这是一个相当长且可能脆弱的XPath。有更好的方法找到这个元素。这是一个相当长且可能脆弱的XPath。有更好的方法找到这个元素。