Not able to identify a lenghty linktext in selenium webdriver - selenium

I want to identify an element by linktext, I am facing strange issue
If the linktext value is short i.e addFirst, addLast will be able to locate the element by using
driver.findelement(By.linktext("addLast, addFirst")).click
IF the linktext is addmanualreferraltocheckthelenghtF, addmanualreferraltocheckthelenghtF lengthy as above not able to identify the element
Please help me to find the solution

Why don't you try partial link text.
driver.findelement(By.partialLinkText("addLast, addFirst")).click

try out contains method. use the XPATH value and in XPATH use contains for link text.
EG:
.//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]
driver.findelement(By.xpath("//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]").click
Alternatively you can also use starts with XPATH value.
.//*[starts-with(#linktext,'addmanual')]
driver.findelement(By.xpath("
//*[starts-with(#linktext,'addmanual')]").click
Hope it helps you.

Related

Selenium C# - I'm unable to find an element on this page using any of the locators

This was just a random script I made to complete a quiz but I can't seem to access the final element. I want to select the element, click the element and then send some text to the element.
I have tried to access the input box by class name, CssSelector and by XPath.
The website is https://www.16personalities.com/free-personality-test
Here are the XPaths I have tried:
//*[contains(#class, 'email-wrapper')]
//div[contains(#placeholder, 'your#email.com')]
//div[#class="row request-info-wrapper"]
//*[#id='request - email']"
Any help is greatly appreciated as I'm new to the framework and would very much like to know what I'm not understanding about locators! Thank you!
EDIT:
I can't seem to target this element or any of its children:
You have selected wrong tag DIV.Try this following Xpath. All should work.
"//input[#id='request-email']"
Or
"//input[#name='email']"
Or
"//input[#placeholder='your#email.com']"
Your field has a (presently) unique ID of "request-email".
Thus you can simply use, as a CSS selector,
('#request-email')
Then, in you can simply tell Selenium to hit ENTER to save your data. Let me know if you need help doing that.

How to write a XPath for the text one4

I want to use XPath to locate a link behind a text.
I want to use XPath to locate a link behind a text. For example, locate "one4" by "what10". You can only use the text message "what10", but you can't use it in any other way, because the information on this page will change. I want to get is the "one4" link node.
<body>
<p>
so
<br>what1 one
<br>what2two
<br>what11one4
<br>what3three
<br>what4one1
<br>what5two2
<br>what6three3
<br>what7one3
<br>what8two3
<br>what9three3
<br>what10one4
<br>just return
<br></p>
</body>
For some special reasons, what I want to pass is that the text of what10 is positioned to one4.
Please help me.
You can use below line
WebElement loginLink = driver.findElement(By.linkText("one4"));
Selenium doesn't supports xpath-2.0 but uses xpath-1.0
The element which you are trying to refer i.e. which contains the text what10 is a Text Node and Selenium can't use it as a reference. So finding the node with text as one4 with reference to the text what10 won't be possible. As an alternative if the desired node is always the last but one node you can use the following solution:
xpath:
driver.findElement(By.xpath("//body/p//a[position()=last()-1]"));
Update
As per #MosheSlavin counter question here is the snapshot to demonstrate that the XPath works perfecto:

Find specific cell of a celltable with Selenium xpath

I have a celltable , where i have multiple rows and is a dynamic table,
Screenshot attached ..
What i want is to find the row where column contains useren#lim.mobi
and then click its checkBox.
I am trying with xpath but not much experience here , If i can get some help please
Thanks
Here is the html code of the specific cell
I don't know the exact html of your table to form an xpath would be difficult.
however it should look something like this
//*[contains(text(),'useren#lim.mobi ')]/../td[2]
for following table, if I have to find the corrosponding contact for some company, This is how I would do it.
https://www.w3schools.com/html/html_tables.asp
//*[contains(text(),'Magazzini Alimentari Riuniti')]/../td[2]
Please try with the following xpath:
//tr[./td[contains(text(),'useren#lim.mobi')]/td[2]/input
if above xpath is not working please attach html code screenshot so i can tell you the extact xpath.
This might help to solve your issue:
//td[#title="useren#lim.mobi"]/following::input[#type="checkbox"]
If you get NoSuchElementException with this XPath you might need to add ExplicitWait as below:
WebElementWait wait = new WebElementWait(getWebDriver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//td[#title='useren#lim.mobi']/following::input[#type='chec‌​k‌​b‌​ox']")));

How to find webelement using style property

Below is the HTML code of an element and I want to locate this element by classs and style property using selenium webDriver in java
<div class="qooxdoo-table-cell" style="left:252px;width:117px;height:24px;"/>
suggest a way which can be help full in selenium
I want to locate the element using java code
i.e. Driver.findelement(by. ....
As long as the element isn't unique you must grab both attributes:
This is the general form, replacing the empty strings for your required class and style:
driver.findElement("By.xpath(//div[#class='' and style='']");
So:
driver.findElement(By.xpath("//div[#class='qooxdoo-table-cell' and style='left:252px;width:117px;height:24px;']");
Best of luck!
If you need to match <div> with exact style attribute, you can try something like
driver.findElement(By.xpath("//div[#class='qooxdoo-table-cell'][#style='left:252px;width:117px;height:24px;']"))
Another way is to use cssSelector as follows:
driver.findElement(By.cssSelector("div[style='left:252px;width:117px;height:24px;']"));
If you are using a older version of selenium you can as well use the older version of cssSelector.
driver.find_elements_by_css_selector("div[style='left:252px;width:117px;height:24px;']")
Note: this function still works in current version of Selenium (with DeprecationWarning )

how to select the check box using selenium?

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.