I am writing automation UI tests for my web page using selenium.
I have an element on the web page which I am testing:
<< input type="checkbox" id="screening_questions[0].multiple_choice[0]-dealbreakerField" value="on" style="position: absolute; cursor: inherit; pointer-events: all; opacity: 0; width: 100%; height: 100%; z-index: 2; left: 0px; box-sizing: border-box; padding: 0px; margin: 0px;>
Since the element has id attribute, so I tried to locate it by using its id value but it didn't work.
If I search for that element in chrome console as:
$('#screening_questions[0].multiple_choice[0]-dealbreakerField')
I get the exception: Uncaught DOMException:
Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.
I thought it will be pretty straight forward to locate it given its id value. Could you please suggest what could be wrong here?
This error message...
Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.
...implies that the Locator Strategy you have adapted is not a valid selector.
As per the HTML you have shared the desired element is a <input> tag with type attribute as checkbox and to use the id attribute you have to escape the . characters and you can use either of the following options :
cssSelector :
"input[id=\"screening_questions[0].multiple_choice[0]-dealbreakerField\"][type='checkbox']"
xpath :
"//input[#id=\"screening_questions[0].multiple_choice[0]-dealbreakerField\"][#type='checkbox']"
Related
Iam unable to get the text value present under strong tag, as after a particular execution or task completion in browser tag gets change.
HTML Code before execution or task completion:
<div>
<strong>heizil</strong>:
<label id="check_label">
<em class="Highlight" style="padding: 1px; box-shadow: rgb(229, 229, 229) 1px 1px; border-radius: 3px; background-color: rgb(0, 191, 255); color: rgb(0, 0, 0); font-style: inherit;" match="test" loopnumber="385144110">Test</em> device testing - Android testing
</label>
HTML code after execution or task completion :
<a id="id_1008326" class="activity">
<strong>heizil</strong> :
<label id="check_label"> "device testing - Android testing"
</label>
using the following code line :
System.out.println(driver.findElement(By.xpath("//a[#class='activity'][contains(.,'Android testing')]/strong")).getText());
I'm getting desired result (heizil),but if i try to get strong tag text value before HTML execution or task completion iam getting no element exception.
Is there anyway we can print (heizil) text under strong tag using ' device testing - Android testing ' as this is the only text constant, irrespective of after execution or before execution.
Note : observed <div> tag before completion of task and after completion of task it changes to another HTML code, could you please help me getting textvalue inside strong tag using text ' device testing - Android testing '
Try with this xpath:
//label[#id='check_label']/preceding-sibling::strong
Or xpath with utilize contains function:
//label[contains(.,'Android testing')]/preceding-sibling::strong
In this page I want to access the Game Locations table :
<h3><span class="mw-headline" id="Game_locations">Game locations</span></h3>
<table class="roundy" style="margin:auto; border: 3px solid #A040A0; background: #78C850; padding:2px; width: 100%; max-width: 740px;">
I use this in order to reach the h3 tag and after get the following sibling element which is the table:
WebElement gameLocationsHeader= driver.findElement(By.xpath("//*[text() = 'Game locations']"));
However , I don't know how to reach the following sibling element (table) , I've tried using :
gameLocationsHeader.findElement(By.xpath("./following-sibling:*"));
But its not working , any hint ?
Node matched by your XPath is span. This element is NOT a sibling of table. So you need to locate h3 which is parent of span and sibling of table
Try below code-lines to get required result:
gameLocationsHeader= driver.findElement(By.xpath("//h3[span='Game locations']"));
gameLocationsHeader.findElement(By.xpath("./following-sibling::table"));
I am writing automation UI tests for my web page using selenium.
I have an element on the web page which I am testing:
<< input type="checkbox" id="screening_questions[0].multiple_choice[0]-dealbreakerField" value="on" style="position: absolute; cursor: inherit; pointer-events: all; opacity: 0; width: 100%; height: 100%; z-index: 2; left: 0px; box-sizing: border-box; padding: 0px; margin: 0px;>
Since the element has id attribute, so I tried to locate it by using its id value but it didn't work.
If I search for that element in chrome console as:
$('#screening_questions[0].multiple_choice[0]-dealbreakerField')
I get the exception: Uncaught DOMException:
Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.
I thought it will be pretty straight forward to locate it given its id value. Could you please suggest what could be wrong here?
This error message...
Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.
...implies that the Locator Strategy you have adapted is not a valid selector.
As per the HTML you have shared the desired element is a <input> tag with type attribute as checkbox and to use the id attribute you have to escape the . characters and you can use either of the following options :
cssSelector :
"input[id=\"screening_questions[0].multiple_choice[0]-dealbreakerField\"][type='checkbox']"
xpath :
"//input[#id=\"screening_questions[0].multiple_choice[0]-dealbreakerField\"][#type='checkbox']"
I have a Web application which implements Canvas tags . Inside one canvas section there are multiple buttons and other elements. I want to click on the button inside that canvas tag using Selenium WebDriver, But unable to find locators(id,xpath etc) using inspect element, selenium IDE or Firepath etc. Even I am unable to see any other tags inside canvas tag.
Is there any way to get the locators of the elements inside canvas tag or any way to interact with these elements using Selenium WebDriver?
canvas tag:
<canvas style="padding: 0px;
margin: 0px; border: 0px none;
background: none repeat scroll 0% 0% transparent;
position: absolute;
top: 0px; left: 0px;
width: 360px;
height: 360px;"
width="360" height="360"/>
Did you try using parent tag in your xpath? Since inside one canvas section there are multiple buttons, you can try /parent::*[canvas attributehere]/parent::*[another-canvas attribute here] .. and so on until you get the specific element
One solution would be using x and y co-ordinates.
driver.switchTo().window(frame);
WebElement canvas= driver.findElement(By.id("canvas"));
Actions action2 = new Actions(driver);
action2.moveToElement(canvas, canvas.getLocation().getX()+ELEMENTS_DISTANCE_FROM_CANVAS_X, canvas.getLocation().getY()+ELEMENTS_DISTANCE_FROM_CANVAS_Y).click().build().perform();
Maybe this Java tutorial could be helpful for someone this is the essential code from it:
var canvas_dimensions = canvas.getSize();
int canvas_center_x = canvas_dimensions.getWidth() / 2;
int canvas_center_y = canvas_dimensions.getHeight() / 2;
int button_x = (canvas_center_x / 3) * 2;
int button_y = (canvas_center_y / 3) * 2;
// Click button on the canvas
new Actions(driver)
.moveToElement(canvas, button_x, button_y)
.click()
.perform();
I am writing code for clicking link which looks as:
<a style="left: 520px; top: 340px; height: 24px; color: blue; position: absolute;" href="Login.aspx?iUserFlag=1">
Text: Sign in as different user?
I have written as:
Selenium.click(“link = Sign in as different user?”);
But i am unable to click, can anyone help on this?
HTML Snippet for this is
<A style="POSITION: absolute; HEIGHT: 24px; COLOR: blue; TOP: 340px; LEFT: 520px" href="http://kaizenblitz/Login.aspx?iUserFlag=1">Sign in as different user?</A>
the error message i got while executing is:
Exception in thread "main" com.thoughtworks.selenium.SeleniumException: ERROR: Element link = Sign in as different user? not found
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:109)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:103)
at com.thoughtworks.selenium.DefaultSelenium.click(DefaultSelenium.java:193)
at seleniumproject.gmail.main(gmail.java:19)
Can any one suggest me why i am unable to click the link?
Make sure that Selenium is your object, and not selenium.
My guess is that your issue is with the spacing.
Change selenium.click("link = Sign in as different user") to...
selenium.click("link=Sign in as different user")
Your test is looking for a link that has the text " Sign in as different user" (note the space at the beginning)