selenium click on anchor tag inside table td

后端 未结 2 1790
梦如初夏
梦如初夏 2021-01-23 14:48

i have my html code looks like this:

相关标签:
2条回答
  • 2021-01-23 15:03

    As you have the link under a tag so selenium must work with below code. You xpath will be .//*[@id='food-search-all']/ul/li (assuming that div id is unique .You can take help with firebug for generate xpath )

    IWebElement TargetElement = driver.FindElement(By.XPath(xPathVal)); 
    string CellVal = TargetElement.Text.ToString(); 
    TargetElement = driver.FindElement(By.LinkText(CellVal));
    TargetElement.Click();
    

    sometimes sigle use of .Click() not work i will suggest add one more TargetElement.Click(); if your first try not work.

    0 讨论(0)
  • 2021-01-23 15:15

    Small error in your code, you're missing '' around the text "+linktext+":

    driver.findElement(By.xpath("/table[contains(@class,'results ib-list')]/tbody/tr/td[@class = 'name']/a[contains(text(),'"+linktext+"')]")).click();
    

    NoSuchElementException usually means the element is in a frame, or slow to load. Selenium only interacts with elements in the current frame. Any element within a child frame cannot be interacted with until you switch into that frame. You can switch by using switchTo().frame():

    driver.switchTo().frame(ARG);
    

    The arguments for frame() are

    • number from 0
    • id of the frame
    • the webelement rerference of the frame

    When done in the iframe, use the following to exit back to the top of the document:

    driver.switchTo().defaultContent();
    
    0 讨论(0)
提交回复
热议问题