Css Selector query | Selenium Forum
M
Posted on 21/11/2015
My code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Driver {
public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
WebDriver cd =null;
cd=new FirefoxDriver();

cd.get("http://yahoo.com");
cd.manage().window().maximize();
cd.findElement(By.id("p_13838465-p")).sendKeys("Games");
Thread.sleep(5000);

WebElement res=cd.findElement(By.cssSelector("*[id^='p_13838465-']"));
System.out.print(res.getText());

}



Output:

Yahoo Search
Search:
Search Web
gamestop
games for girls
gamesgames.com
gamestop black friday deals 2015
games free
games for kids
gamestop locations
games workshop
games for boys
games2girls




question : When i m using
WebElement res=cd.findElement(By.cssSelector("*[id^='p_13838465-']"));

It should return 1st element whose id starts with p_13838465-.
why it is returning all elements??
as i can also write String res=cd.findElement(By.cssSelector("*[id^='p_13838465-']"));
System.out.print(res);

Please explain


opened yahoo.com and typed games in search box,

Screenshot attached

M
Replied on 23/11/2015

use robot framework, it has been explained in the videos.


M
Replied on 23/11/2015

What are we going to achieve here with Robot Framework? Could you please explain?


M
Replied on 23/11/2015

I can use robot framework. But please explain reason for this behavior


M
Replied on 23/11/2015

could you please repeat the question. and those id will keep changing. please find more stable element.


M
Replied on 24/11/2015

Hi ,

My query is simple.i am not concerned about output.

below code:

String res=cd.findElement(By.cssSelector("*[id^='p_13838465-']"));
System.out.print(res);


ideally this command should return 1 value ( 1st value being matched) . but i m getting list of values.

say we have 10 items with xpath "*[id^='p_13838465-']"). I am using find element ..it should give 1 st value ..why it is giving all values ..


isn't it contradictory?


M
Replied on 24/11/2015

it seems like "[id^='p_13838465-']" is very common part of id.

what is the full path?

"[id^='p_13838465-results']/li[1]/a"


M
Replied on 25/11/2015

I am not concerned about full path.

My query is for the path i gave , I should get 1 value.
Why i m getting list of values as I m using find element not find elements


M
Replied on 25/11/2015

[quote="Sheenam":2te9oitg]

My query is for the path i gave , I should get 1 value.
[/quote:2te9oitg]

you're not getting 1 value because id is common with all the list. to fix it give a full path.