【问题标题】:how can i click on image using selenium webdriver in c#?如何在 c# 中使用 selenium webdriver 单击图像?
【发布时间】:2018-09-22 17:40:43
【问题描述】:

我正在尝试使用 selenium webdriver 和 c# 实现自动测试。 我的页面右下角有这个用 JavaScript 制作的图片按钮。

我的 HTML 代码:

<html>
<head>
  <meta charset="utf-8">
  <title>Sanofi - Digital Value Chain</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <meta name="description" content="">
  <meta name="author" content="">
  <link rel="icon" type="image/x-icon" href="assets/favicon-sanofi.png">

<link href="styles.e3154dfb8e8b22c30979.bundle.css" rel="stylesheet"/></head>
<body>
  <app-root>Automatic testing page - Chatbot with Selenium + C# </app-root>

  <!-- Avaamo Chatbot -->
	<script type="text/javascript">
		var AvaamoChatBot=function(t){function o(t,o){var n=document.createElement("script");n.setAttribute("src",t),n.onload=o,document.body.appendChild(n)}return this.options=t||{},this.load=function(t){o(this.options.url,function(){window.Avaamo.addFrame(),t&&"function"==typeof(t)&&t(window.Avaamo)})},this};
		var chatBox = new AvaamoChatBot({url: 'https://c0.avaamo.com/web_channels/47afd7fd-6473-445a-bb87-4b4c46c51b94?locale=eng&bot_typing_duration=60000'});
		chatBox.load();
	</script>
<script type="text/javascript" src="inline.c239ca988071a28dfcf5.bundle.js"></script><script type="text/javascript" src="polyfills.1c766e36408e34083505.bundle.js"></script><script type="text/javascript" src="scripts.c7b8f195ebee14d64454.bundle.js"></script><script type="text/javascript" src="vendor.b1d0922e092ba105d6d7.bundle.js"></script><script type="text/javascript" src="main.44cb2cdc7217ee361473.bundle.js"></script></body>
</html>

所以,我正在尝试使用此 C# 代码,但它不起作用。

//Create the reference for our browser
IWebDriver driver = new ChromeDriver();

//Navigate to LANDING PAGE
driver.Navigate().GoToUrl(BOT_ADDRESS_LOCAL_DISC);

//Maximize window       
driver.Manage().Window.Maximize();

//Aguarda 3 segundos até carregar o BOT
System.Threading.Thread.Sleep(10000);

//Find and click on CHATBOT Button
driver.FindElement(By.XPath("text/javascript")).Click();

//Wait until load all details.
System.Threading.Thread.Sleep(2000);

有人有解决办法吗???

【问题讨论】:

    标签: c# selenium webdriver


    【解决方案1】:

    您可以使用 cssSelector:

    driver.FindElement(By.CssSelector("script[type='text/javascript']")).Click();
    

    【讨论】:

    • 执行此操作时,我的 C# 代码出现错误。 OpenQA.Selenium.ElementNotVisibleException: '元素不可见 (会话信息: chrome=65.0.3325.181) (驱动信息: chromedriver=2.37.543627 (63642262d9fb93fb4ab52398be4286d844092a5e),platform=Windows NT 10.0.16299 x86_64)'
    • 似乎您需要等到元素可见才能与之交互。有一个“预期条件”类可以做到这一点。
    • 是的,您可以增加它,但不要使用 System.Threading.Thread.Sleep(10000),而是使用 ExpectedConditions 类,这样您只需等待元素可用,而不仅仅是等待固定时间。
    • 你能给我举个例子吗????我仍在尝试实现这一点,但即使现在也没有成功。
    【解决方案2】:

    要点击图片按钮,您必须诱导WebDriverWait,您可以使用以下代码行:

    IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3))
    wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//link[@rel='icon' and contains(@href,'favicon-sanofi.png')"))).Click();
    

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 2017-12-26
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 2016-08-31
      • 1970-01-01
      • 2013-10-19
      相关资源
      最近更新 更多