Selenium: find part of identifier - java

The next command does not work
wd.findElement(By.id("post_message_*"));
The goal is to find id's with post_message_3456346, post_message_01548 and so on. How to fix it?
Actulay there is only one id on page which starts with "post_message_".

Try to locate required element by XPath as below:
wd.findElement(By.xpath("//*[starts-with(#id, 'post_message_')]"));
or by CSS selector
wd.findElement(By.cssSelector("*[id^='post_message_']"));

Try using a css Selector
wd.findElement(By.cssSelector("[id^='post_message_']"));

Related

Unable to find an element (Selenium, Java)

I am trying to the following element:
<span data-dojo-attach-point="lN" role="btn" aria-selected="false" class="xTreeNLbl">Find</span>
The following is the Java code:
private WebElement search_btn = driver.findElement(By.xpath("//div[#data-dojo-attach-point='lN' and contains(text(),'Find')"));
search_btn.click();
It's unable to find the element. Please help. Thanks.
you are wrong at the xpath //div
as this is <span> tag not <div> tag
you should write like this,
driver.findElement(By.xpath("*//span[text()='Find']")).click();
Your xpath is almost correct, you just need to correct tag name with Span
driver.findElement(By.xpath("//span[#data-dojo-attach-point='lN' and contains(text(),'Find')"));
One option may be to try and find the element using one of it's attribute, like its class if it's unique.
Taken from this previous question (Find element by attribute), the formula is:
element[attribute='attribute-value']
So if you have,
You can find it using:
By.cssSelector("a[href='mysite.com']");
Please try with the follow code:
//span[#role='btn' and #class='xTreeNLbl']
If this code does not solve your error, pass me the html dom code of the page and I will help.

How to click a button where the class is the same for another options?

I am trying to click a button from a list but this button has the same class than others in the list because they have the same name (btn ban-red) so how can I click it if in the inspect I have this information:
<a class=“btn ban-red” data-track-event=“navigate” data-track=name=“Jobylon” - Quality Engineer” href=“https://emp.jobylon.com/jobs/16654-f/” target=“_blank”>View job/a>
The inspect is copying this xpath:
/html/body/div[1]/div[4]/div/div/div/div[3]/div/div/div/div[1]/section/div/div[2]/div[1]/div[1]/article[14]/a
But it is not working
I also created my own xpath this way:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-track-name=‘Jobylon - Quality Engineer’]"))).click();
But is not working either
I am using Selenium with java and I am in a Macbook, thank you for your help.
Absolute xpath not recommended. You can try using relative xpath.
Locate based on element text
driver.findElement(By.xpath("//a[contains(.,'View job')]")).click()
Locate using combination of element attribute if classes are not unique
driver.findElement(By.xpath("//a[#class='btn ban-red'][#data-track-event='navigate']")).click()
OR
driver.findElement(By.xpath("//a[#class="btn ban-red"][#href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Better to use CSS selector as its faster then xpath. So you try like
driver.findElement(By.cssSelector("a[class='btn ban-red'][data-track-event='navigate']")).click()
OR
driver.findElement(By.cssSelector("a[class="btn ban-red"][href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Still facing some issue like element not visible or no such element then try with explicit wait conditions until your element gets visible or clickable.

how to locate ::Before element in Selenium Java

unable to click on this specific "X" element
try with following xpath, It may works.
//*[#id='ngdialog1']//*[#class='ngdialog-close']
In java
driver.findElement(By.xpath("//*[#id='ngdialog1']//*[#class='ngdialog-close']").click();
try using following code:
driver.findElement(By.xpath("//table[#id='ngdialog1']//table//following-sibling::div[1]").click();

How can I get text from element with Selenium webdriver and Java?

My code:
WebDriver driver = new SafariDriver();
driver.get("http://bet.hkjc.com/football/default.aspx");
WebElement matchs = driver.findElement(By.cssSelector("span.Head to Head"));
System.out.println(matchs);
driver.quit();
How can I crawl Manchester Utd and Celta Vigo?
WebElement matchs = driver.findElement(By.xpath("//a[#title='Head to Head']"));
System.out.println(matchs.getText());
Use firebug and firepath addons in firefox and inspect that element and get the xpath and put it here inside double quotes in this code :
System.out.println(driver.findElement(By.xpath("")).getText());
If you don't know how to use firebug and firepath refer this link
You can locate the element either by css selector or xpath selector
By using xpath
driver.findElement(By.xpath("//a[#title='Head to Head']"));
By using css Selector
driver.findElement(By.cssSelector("span > a[title='Head to Head']"));
OR Try somethings like this if not getting the match
driver.findElement(By.cssSelector("td.cteams.ttgR2>span>a[title='Head to Head']"));
Note : in your code you are trying like span.Head to Head in CSS selector . dot represents the class and according to your path you are locating span tag which have class name "Head to Head" which doesn't exist in your dom as this is the title of anchor tag.
Went through the Firebug and Firepath plugins of Firefox initially to get the Xpath or css path
Explore some blogs to get clear understanding, you will be able to create by yourself
Refer This link for the same
I assume all the above answers doesn't work for you and am providing another answer.
I can see both the texts are under "a" tag. So the idea is to navigate to the element and use getText() - which returns the visible text.
String word = driver.findElement(By.xpath("//span/a")).getText();
System.out.println(word);
Hope this works for you.
In all of my tests I'm using the getAttribute like this to get text and it is working fine for me on all drivers :
assertEquals(strCity, txtCity.getAttribute("value"));

Java Selenium. Find element by text for certain xpath

Trying to find an xpath expression to use in:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPATH HERE"))).click();
The element says "Invite Users" on the page and i need to be able to click on it
I need to find the element /a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle'] but the problem is the characters after "inviteUsers_" is dynamically generated
I have already tried these:
"//*[contains(.,'Invite Users')]";
"//a[contains(.,'Invite Users')]";
And these give NoSuchElement exceptions.
This is the complete XPATH:
/html/body/div[#class='col-xs-10 col-xs-offset-2 main']/fieldset[#class='form-horizontal']/div[#id='roles']/div[#id='entitlements']/div[#class='panel panel-default '][3]/div[#id='service_8ef17ba4-b739-4fb6-8198-3862ea84c381']/div[#class='panel-body']/div[#class='panel panel-default'][1]/div[#class='panel-heading']/h4[#class='panel-title']/a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle']
You can solve it with starts-with():
//a[starts-with(#id, "inviteUsers_")]
If this does not work try find a unique parent.
//div[contains(#id, 'service')]//a[contains(#id, 'inviteUsers')]

Categories

Resources