找到你要的答案

Q:Click on hyperlink using Selenium Webdriver

Q:使用超链接点击是WebDriver

I am having a problem while trying to click on a hyperlink using Selenium Webdriver. I tried using CssSelector as well as XPath and nothing seem to work. All I am trying to do is Click on the Google text.

    <div class="hello">
    <div class="test">
    <table class="first first-table">
    <tbody>
    <tr>
    <td class="second-table">
    <a id="dummyID" href="https://www.google.com" target="_blank">Google</a>
   </td>
   </tr>
  </tbody>
  </table>`

I tried driver.FindElement(By.XPath("//*[@id='dummyID']")).Click(); I also tried using href to find the link...pretty much everything. All the time I am getting "unable to locate element" exception. Anyone has any suggestions?

我有一个问题,在点击一个超链接使用Selenium WebDriver。我试着用cssselector以及XPath和似乎没有工作。我所要做的就是点击谷歌文本。

    <div class="hello">
    <div class="test">
    <table class="first first-table">
    <tbody>
    <tr>
    <td class="second-table">
    <a id="dummyID" href="https://www.google.com" target="_blank">Google</a>
   </td>
   </tr>
  </tbody>
  </table>`

我试着司机。findelement(由XPath(“/ / * [ @ ID = 'dummyid“]”))。();我也试过使用href找到链接…几乎一切。我一直在“无法定位元素”异常。有人有什么建议吗?

answer1: 回答1:

Try this:

driver.FindElement(By.LinkText("Google")).Click();

Look to see if the element is within a frame or iframe. If it is, you'll need to use:

driver.SwitchTo().Frame("frameID");

试试这个:

driver.FindElement(By.LinkText("Google")).Click();

看看这个元素是在一个框架或iframe。如果是,你需要使用:

driver.SwitchTo().Frame("frameID");
answer2: 回答2:

You can easily find by id:

driver.FindElement(By.id("dummyID")).Click();

Or you can use this xpath in your scenario:

driver.FindElement(By.XPath("//a[@id='dummyID']")).Click();

You can directly find the element by id, it is an easy and efficient approach. I hope you will get your desired click on it with any of these code fragments, both should work the same. Surely it will work.

你可以很容易地找到ID:

driver.FindElement(By.id("dummyID")).Click();

或者你可以在你的场景中使用XPath:

driver.FindElement(By.XPath("//a[@id='dummyID']")).Click();

You can directly find the element by id, it is an easy and efficient approach. I hope you will get your desired click on it with any of these code fragments, both should work the same. Surely it will work.

answer3: 回答3:

You can use

driver.FindElement(By.LinkText("Google")).Click();

你可以使用

driver.FindElement(By.LinkText("Google")).Click();
xpath  selenium