I am trying to make some automation software automating Chrome. I can navigate to the page, and the page has a button declared like this in the code:
<div class="bn_diff_image">
<a href="javascript:setMode('ImageText', 0);"></a>
</div>
The class shows an image of a button. I need to click that button (to execute the javascript) but I can't get it to work. Here is the code I have tried:
chromeDriver.ExecuteJavaScript("javascript:setMode('ImageText', 0)")
This returns an error that setMode cannot be found. I put a Thread.Sleep in the code to ensure the page was fully loaded before calling this, it made no difference
I tried accessing the button via CSS selector
chromeDriver.FindElement(By.CssSelector("a[href*=\'ImageText', 0']")).Click()
It can't find the selector and fails
I tried finding the div by classname and clicking on that
chromeDriver.FindElement(By.ClassName("bn_diff_image")).Click()
'
but it fails saying it can't find the element
How in the world can I click on this link or run that javacript on the page?
CodePudding user response:
Please try this css selector :
div.bn_diff_image a[href^='javascript:setMode']
use it like this :
chromeDriver.FindElement(By.CssSelector("div.bn_diff_image a[href^='javascript:setMode']")).Click()
CodePudding user response:
Never Mind, I am bad at this, The element was in an iFrame. Once I elected the Frame it worked