I am trying to locate those buttons as shown here:
And this is HTML for the same :
I tried with all possible ways but nothing works.
As per the HTML you have shared you can locate the Continue with New button with either the following options :
XPATH
//button[#class='slm-btn slm-btn-AdvanceFlow' and #id='Continue']/span[#class='slm-btn-text']
CSS_SELECTOR
button.slm-btn.slm-btn-AdvanceFlow#Continue > span.slm-btn-text
First, you have to try with id because it is the best way to get it because it is unique.
Otherwise, you can use XPath:
//button[#id='ContinueNew']
//button[contains(#id,'ContinueNew')]
Related
I am trying to write a function in selenium to check if a Reasons dropdown is showing as disabled, but can't quite get the xpath right. The code for the dropdown is in the pic, the function I'm working on is the second one (InputDisabled), having based it on the working first one (SearchDisabled):
` public By SearchDisabled(string searchId) => By.XPath($"//div[#id='{searchId}']//div[contains(#class, 'v-input--is-disabled')]");
public By InputDisabled(string inputId) => By.XPath($"//div[#id='{inputId}']//div[contains(#class, 'v-input--is-disabled')]");`
The inputId going into it is 'ai-confirm-allergy-modal-reason'. I've tried it as 'input[contains...' and 'contains(#disabled, 'disabled'...' among other things, but my xpath knowledge isn't great yet!
dropdown code
Use below code
String value = driver.findElement(By.XPath("//input[contains(#id, 'ai-confirm-allergy')]").getAttribute("disabled");
Assert.AssertEquals(value, "disabled");
I do not quite get your question.
well if you are trying to use xpath to locate an element, you can just use the id; assuming that it is unique.so:
driver.findElement(By.xpath("//input[contains(#id, 'ai-confirm-allergy')]")
should locate the webelement.
However, your xpath for the SearchDisabled is locating a div containing class 'v-input--is-disabled' with in another div with id of '{searchId}';
the same logic goes for the next one. your xpath is trying to locate a div containing class 'v-input--is-disabled' which is located with in another div located using input id. I don't think this combination can locate the element highlighted in the picture.
I am trying to click an element with text "No, Thanks" using below code, I tried various options so far such as
driver.findElementByXPath("//*[contains(text(),'THANKS')]").click();
driver.findElement(By.name("No,THANKS")).click();
driver.findElementByName("No,THANKS").click();
There is no other element with same text. I am using Appium Driver and Samsung device.
If it is TextView, you can consider the following
driver.find_element_by_xpath("//android.widget.TextView[#text='No, Thanks']")
Seems you were close. The text No, Thanks contains a , character in between which you need to avoid. So effectively you can use either of the following xpath based Locator Strategies:
xpath 1:
driver.findElementByXPath("//*[starts-with(., 'No') and contains(., 'Thanks')]").click();
xpath 2:
driver.findElementByXPath("//*[contains(., 'No') and contains(., 'Thanks')]").click();
Please try this:
driver.findElement(By.xpath("//android.widget.TextView[contains(#text,'No, Thanks')")).click();
In my webdriver test I have a web page with a tag named c-wiz with the class="boqChromeogbviewView_ boqChromeapiViewView_ modeChromeScrollable_". I need to send an ESC key press to this element since it has a listener for it. So I tried
driver.findElement(By.xpath("//c-
wiz[#class='boqChromeogbviewView_']")).sendKeys(Keys.ESCAPE);
but it fails saying
Unable to locate element: {"method":"xpath","selector":"//c-wiz[#class='boqChromeogbviewView_']"}
Can someone please help?
With xpath you have to use whole class value or use contains like below:
driver.findElement(By.xpath("//c-
wiz[#class='boqChromeogbviewView_ boqChromeapiViewView_ modeChromeScrollable_']")).sendKeys(Keys.ESCAPE);
driver.findElement(By.xpath("//c-
wiz[contains(#class,'boqChromeogbviewView_']")).sendKeys(Keys.ESCAPE);
Use css selector instead:
driver.findElement(By.cssSelector("c-
wiz.boqChromeogbviewView_")).sendKeys(Keys.ESCAPE);
I am trying to do SignIn for this. In it click on 'SignIn' which I have done it successfully. Now when trying to type in Username/Password using Xpath it shows exception which says
Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: element not visible
code is :-
driver.findElement(By.xpath(".//*[#id='load_form']/fieldset[1]/input")).sendKeys("test123");
driver.findElement(By.xpath(".//*[#id='load_form']/fieldset[2]/input")).sendKeys("test123");
I know this Xpath is same as used in SignUp form, so what are the other ways to locate these two fields? How we can use it by using cssselector?
Thanks for the help in advance.
Go One Level up on finding the relative xpath with
//div[#id='login']/form[#id='load_form']//input[#name='username']
//div[#id='login']/form[#id='load_form']//input[#name='password']
Try this
//For Username
driver.findElement(By.xpath("(//input[#name='username'])[2]")).sendKeys("username test");
//or
driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='usernam']")).click();
//For password
driver.findElement(By.xpath("(//input[#name='password'])[2]")).sendKeys("password test");
//or
driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='password']")).click();
the above codes are working for me.
There are basically two elements found by provided xpath, and it works with first found element which is invisible as your exception saying, you should try using cssSelector as below :-
driver.findElement(By.cssSelector("div#login input[name = 'username']")).sendKeys("test123");
driver.findElement(By.cssSelector("div#login input[name = 'password']")).sendKeys("test123");
Note:- For learning about cssSelector follow this url and for xPath follow this url
I'm suggesting you before selenium execution with the locator you need to sure that using locator is correct and returns single or multiple element at your browser console by pressing f12. for verify xPath you should use $x("your xpath expression") and for cssSelector you should use document.querySelector("your css selector expression")..
Hope it will help you..:)
How to select the checkbox which has a dynamically changing ID and XPath?
Multiple ways:
You should look at a pattern like id or name somoething like
CT_CHKBox_157, CT_CHK_158 etc.. For example, to click the first
Checkbox having a pattern of Ids
You can use a dynamic xpath like driver.findelement(By.xpath(//input[starts-with(#id,'CT_CHK'][1]).click()
Identify the Unique Element which are close ancestors to the
Checkbox in question and reach out to it through xpath or css path
relatively or through indexing from within.
Hope that clarifies.
Have you tried XPath by position? Ultimately the check boxes are like buttons or link that can be clicked so driver.findElement(By.xpath("//xpath by position")).click();
Alternativey you might want to use JavaScript:
((JavascriptExecutor) driver).executeScript("return document.getElementsByName('ChkboxValue')[0].checked;");
Hope this helps.
Selenium uses what is called locators to find and match the elements.There are 8 locators strategies included in Selenium:
Identifier
Id
Name
Link
DOM
XPath
CSS
UI-element
you can try using any other Locator in the list.