Unable to locate an element implemented as Span with Selenium WebDriver (Java) - java

When recorded in Selenium IDE, the result is this:
click
//span[#id='some_text']/table/tbody/tr/td/table/tbody/tr[2]/th/hd/font/span/acronym
Here is my entry in Eclipse using WebDriver(Java)
driver.findElement(By.xpath("//span[#id='some_text']/table/tbody/tr/td/table/tbody/tr[2]/th/h2/font/span/acronym")).click();
Here is the error I receive:
no such element: Unable to locate element:
{"method":"xpath","selector":"//span[#id='some_text']/table/tbody/tr/td/table/tbody/tr[2]/th/h2/font/span/acronym"},
HTML
<span onclick="change_site"('SITES');" style="cursor:pointer; color:#12345; text-decoration:underline;">
<acronym title=Site ID:">DEFAULTSITE</acronym>
</span>

As per the HTML provided you can directly access the acronym element
driver.findElement(By.xpath("//span/acronym[#title='Site Id:']")).click();

Try with below xPath :
driver.findElement(By.xpath("//acronym[contains(text(),'DEFAULTSITE')]").click();

Try any of the below xpath
driver.findElement(By.xpath("//acronym[text()='DEFAULTSITE']");
driver.findElement(By.xpath("//span/acronym[text()='DEFAULTSITE']");
Or this css
driver.findElement(By.css("acronym[title='Site']");

If none of the above Xpath has worked for you ,please try the following Xpath
//span[#id='some_text']/table/tbody/tr/td/table/tbody/tr[2]/th/hd/font/span/*[name()=acronym]
Please let me know if this has helped you or not.

Related

XPATH for href link isnt working. Getting invalid selector: It should be an element

I am trying to automate couple of Selenium scenarios from this website
i was trying to click on the "categories" link by giving the below XPATH
//a[contains(text(), 'CATEGORIES')]/#href
I am getting InvalidSelector error. Could you please suggest an alternative XPATH for this?
Just for your reference:
<ul>
<li>HOME</li>
<li>CATEGORIES</li>
<li>BRANDS</li>
<li>PRODUCTS</li>
<li>MY CART</li>
<li>TRACKING</li>
<li>ACCOUNT
</li>
<li>FAQ'S</li>
<li>ABOUT US</li>
</ul>
Alternative way by using linktext:
=> By.linkText("CATEGORIES")
driver.findElement(By.linkText("CATEGORIES")).click();
Use action class to click on the element.
Actions action=new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//a[contains(text(), 'CATEGORIES')]"))).click().build().perform();
Please note: use following import.
import org.openqa.selenium.interactions.Actions;
"CATEGORIES" section on the page that you are trying to automate is stored as a text in the html structure, so you can find it using the below xpath which finds it by matching its text:
WebElement categoriesSection = driver.findElement(By.xpath("//a[text()='CATEGORIES']"));

Cant find element by xpath to select

Hi im having serious issues trying to locate this element using an xpath:
class="accountSettingsTextBase_1u09j40-o_O-accountSettingsItemText_10y8axf" aria-current="false" href="/account/signin">Log In
I've tried:
"//*[contains(text(),'Log In')]"
and also the actual xpath:
//*[#id='root']/div/header/div[3]/nav/ul/li[1]/a
as well as link text which still won't work, any ideas??
The anchor tags are missing from the HTML by the way, as it was just showing "Log In" with them added in
Full HTML:
Full HTML
Possible xpaths :
//a[#href='/account/signin']
OR
//a[contains(.,'Log In')]
OR
Try using Css selector :
a[class^='accountSettingsTextBase_' ][class*='accountSettingsItemText']
//[contains(text(),'Log In')]
You're missing something:
//*[contains(text(),'Log In')]
Why don't you use
driver.findElement(By.linkText("Log In"))
or
driver.findElement(By.partialLinkText("Log In"))
since the element is an anchor tag?
Please add the explicit wait and then check
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'Log In')]")));
driver.findElement(By.xpath("//*[contains(text(),'Log In')]")).click();

XPATH search for Selenium Testing in JAVA

The problem that I am facing is that I can't seem to select that div class using XPath.
<div class="_3WZoe"><span dir="auto" class="">We can not found, try again.</span></div>
How can i do XPath, i don't know.
Thank You.
//div[contains(#class,"_3WZoe")]

Selenide doesn't find the Web element

Could somebody help to understand what is wrong with element searching via Selenide.
I have such HTML code:
<div>
Current method:
<strong>
SMS
</strong>
</div>
The Selenium finds the element throw this xpath
findElement(By.xpath("//div[contains(. , \"Current method\")]/strong"))
but Selenide returns ElementNotFound exception with the same locator
$(By.xpath("//div[contains(. , \"Current method\")]/strong"))
If you need strong, you can just
$(By.xpath("//strong[contains(text(),'SMS')]"))
You can us following option.
$("strong:contains('SMS')"); Using css selectors method
You can try :
$(byText("Current method:")).shouldBe(Condition.exist);
Using Selenium->
findElement(By.xpath("//div[contains(text(), 'Current method')]/strong"));
Using Selenide->
$(By.xpath("//div[contains(text(), 'Current method')]/strong"));
These two method can be used in selenium as well as selenide.Methods are not specific to Selenium or selenide

How to locate a span with a specific text in Selenium? (Using Java)

I'm having trouble locating a span element in Selenium using java.
the HTML looks like:
<div class="settings-padding">
<span>Settings</span>
</div>
And I've tried the following with no luck:
By.xpath("span[.='Settings']")
and
By.xpath("span[text()='Settings']")
and
By.cssSelector("div[class='settings-padding']"))
as well as some other similar attempts. Could you point me to the best method to do this? As it stands I constantly get "Unable to locate element" error in eclipse.
Your all xpath are looks OK, Just some syntactically incorrect. you are missing // in your xpath
The correct xpath are as below :-
By by = By.xpath("//span[.='Settings']")
Or
By by = By.xpath("//span[text()='Settings']")
Or
By by = By.xpath("//div[#class='settings-padding']/span"))
Or you can use cssSelector as :-
By by = By.cssSelector("div.settings-padding > span"))
Using anyone of the above By locator you can locate element as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(presenceOfElementLocated(by));
Hope it helps...:)
For the element below
<span class="test-button__text">
Test Text
</span>
The following solution works for me
driver.find_element_by_xpath("//span[contains(#class, 'test-button__text') and text()='Test Text']")

Categories