Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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# 使用Selenium单击下拉按钮_C#_Selenium - Fatal编程技术网

C# 使用Selenium单击下拉按钮

C# 使用Selenium单击下拉按钮,c#,selenium,C#,Selenium,我需要单击tweet中的下拉按钮,然后使用SeleniumWeb驱动程序单击“嵌入tweet”选项 我在网上找到了这些代码,并试图实现这些代码,但第三行代码不起作用: driver = new ChromeDriver(@"C:\ChromeDriver 75.0.3770.90"); driver.URL= "https://twitter.com/TheScaryNature/status/1170560743516692480"; driver.FindElement(By.XPath(

我需要单击tweet中的下拉按钮,然后使用SeleniumWeb驱动程序单击“嵌入tweet”选项

我在网上找到了这些代码,并试图实现这些代码,但第三行代码不起作用:

driver = new ChromeDriver(@"C:\ChromeDriver 75.0.3770.90");
driver.URL= "https://twitter.com/TheScaryNature/status/1170560743516692480";
driver.FindElement(By.XPath("//input[@id='']")).Click();

但它不起作用。我是初学者。请帮忙。

可能有两个原因(据我所知):

  • 您试图在元素正确显示在网页上之前单击。在这种情况下,您可以使用wait等待元素实际出现在网页上:
  • 您尝试单击的元素可能位于一个框架中,然后您必须切换到该框架(假设您的框架名称为investigationFRAME):
  • 您尝试使用错误的xpath请尝试以下代码:

    IList<IWebElement> tooltip= driver.FindElements(By.XPath("//div[@class='IconContainer js-tooltip']/span[@class='Icon Icon--caretDownLight Icon--small']"));
    tooltip.Click();
    
    IList tooltip=driver.FindElements(By.XPath(“//div[@class='IconContainer js tooltip']]/span[@class='Icon-Icon-caretDownLight-Icon-small']);
    工具提示。单击();
    
    您可以通过以下代码选择按钮:- driver.findelelement(By.xpath(“/*[@id='menu-0']”)。单击()

    单击按钮后,您可以单击嵌入推文:-
    driver.findelelement(By.xpath(“//button[contains(text(),'Embed Tweet')]”)。click();

    By.xpath
    用作css选择器。由于并非每个元素都有ID,而且在我看来,twitter上的下拉菜单似乎没有ID,所以您可以尝试使用

    driver.findElement(By.XPath("//li[@class='embed-link']/button[@class='dropdown-link']"))
    
    //li/按钮
    表示
    li>按钮
    (li元素内的按钮)


    第一个等待元素出现

    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(//button[@id='menu-0']///span[@class='Icon-Icon--caretDownLight-Icon--small']))

    然后


    driver.findElement(By.xpath(“//button[@id='menu-0']]///span[@class='Icon-Icon--caretDownLight-Icon--small'])。单击

    是否会收到任何错误消息?只说它不起作用会少一点…嗨@Daniel,谢谢你的回复。我收到消息-'没有这样的元素:找不到元素'嗨,我收到这个错误-找不到元素:{“方法”:“xpath”,“选择器”:”//li[@class='embed-link']/button[@class='dropdown-link']]“}您好,我收到了这个错误-'没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:“/*[@id='menu-0']”您好,请您分享元素详细信息,以便我验证。您是否检查了框架?您是否可以发布您正在尝试的网页的URL?
    IList<IWebElement> tooltip= driver.FindElements(By.XPath("//div[@class='IconContainer js-tooltip']/span[@class='Icon Icon--caretDownLight Icon--small']"));
    tooltip.Click();
    
    driver.findElement(By.XPath("//li[@class='embed-link']/button[@class='dropdown-link']"))