This question received great answers in jquery and I was wondering if someone could give an example of this in Java please?
I'm doing driver.findElement(By.className("current time")).click(); The space is the issue, and I see the explanation at the link, but I'm not sure how to handle it in java, and don't have access to change the class name.
Pasting example of what i get in the firefox inspect id: Example with cssSelector below did not work, but i may be missing something.
<span>
<a class="current time" href="http://someurl/" onclick="s_objectID="http://someur/">url</a>
</span>
Instead of class name you can use a css selector. You don't mention the tagname for the class 'current time'. I am assuming it to be input, so your css selector work be,
WebElement element = driver.findElement(By.cssSelector("input[class='current time']"));
element.click();
Edit#1 Based on html provided,
Looking at the html in your comment, it seems you have quite a few options to find the webElement. Here are your options,
WebElement element = driver.findElement(By.cssSelector("a[class='current time']"));
element.click();
or this should work too,
WebElement element = driver.findElement(By.cssSelector("a.current.time"));
element.click();
You can also use linkText since the element is link. From the html you provided, the link text is 'url'
WebElement element = driver.findElement(By.linkText("url"));
element.click();
You can also use By.partialLinkText("partial link text here");
You can also use xpath as:
WebElement element = driver.findElement(By.xpath("//a[#class='current time']"));
element.click();
OR,
WebElement element = driver.findElement(By.xpath("//a[text() = 'url']"));
element.click();
For a less fragile test, another option is to use an XPATH which doesn't depend of the order of classes, like:
WebElement element = driver.findElement(By.xpath("//a[contains(#class, 'current') and contains(#class, 'time')]"));
Whenever you found some space in the class name you need to switch to cssSelector Locator.
Convert a class name to cssSelector if it is having a space as below.
In your case it would be:
WebElement element = driver.findElement(By.cssSelector(".current.time"));
element.click();
PS: add . [dot] in start of class name and replace the space with . [dot] to convert class name to cssSelector.
Related
I am using Selenium with Java and I have encountered issue that I am unable to find div by class name, even though it is unique:
<div class="123123-randomclassname"></div>
I am able to find any other element, e.g. input, button, etc. I have issues with div tag only.
I have tried getting this web element using either #FindBy() annotation and findElement() method:
driver.findElement(By.className("123123-randomclassname"))
driver.findElement(By.cssSelector("div[class='123123-randomclassname'"))
#FindBy(className = "123123-randomclassname")
#FindBy(css = "div[class='123123-randomclassname'")
Any of these solutions did not work and I couldn't find element.
Try with following css selector
driver.findElement(By.cssSelector("div[class^='123123-']"))
//# and ]closing bracket was missing in syntax
WebElement DivTag = driver.findElement(By.xpath("div[#class='123123-randomclassname']"));
//OR
#FindBy(xpath = "div[#class='123123-randomclassname']") WebElement DivTag2;
The element I got is <div class="x-tool x-box-item" id="tool-123" src="data:image/gif;base64" class="new-img x-tool-maximize".
I particularly need this class="new-img x-tool-maximize" because its the common of all the screen.
I already tried
driver.findElement(By.className("new-img.x-tool-maximize")).click()
and
driver.findElement(By.className("new-img x-tool-maximize")).click();
and
driver.findElement(By.xpath("//div[contains(#class, 'value') and contains(#class, 'test')]"))``;
You should be able to use a CSS selector to find this type of element. You’ll want something like driver.findElement(By.cssSelector("div.new-img.x-tool-maximize")).
You can use list in selenium if you have multiple elements with same locators
List<WebElement> classes=driver.findElements(By.classname("new-img x-tool-maximize));
// if you want click 1 st class name element use this following line
classes.get(0).click();
OR // if you want click 2 nd class name element use this following line
classes.get(1).click();
Use a list to get all the WebElements with specific classes
List<WebElement> list = driver.findElements(By.cssSelector("div.new-img.x-tool-maximize"));
As you intent to click() on the element using only the className attribute values, ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector using only the className attribute values:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector(".new-img.x-tool-maximize"))).click();
For a more canonicl approach you can indlude the tagName as follows:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.new-img.x-tool-maximize]"))).click();
References
You can find a couple of relevant detailed discussions in:
Unable to locate element using className in Selenium and Java
How to locate the last web element using classname attribute through Selenium and Python
What are properties of find_element_by_class_name in selenium python?
I have a div inside a div inside another div. Most outer div class is "Big Div", inside it there is a div with class "Medium Div" and the most inner div class is "Small Div".
I'm able to see the div's classes when I press the F12 key and hover over the elements, however I can't find them using Selenium.
What am I doing wrong?
WebElement big = browser.findElement(By.cssSelector("//div[contains(#class,'Big')]"));
WebElement medium = big.findElement(By.cssSelector("//div[contains(#class,'Medium')"));
WebElement small = medium.findElement(By.cssSelector("//div[contains(#class,'Small'"));
Note: my classes contain white spaces, Selenium can't find any of the divs and I get the exception: "No Such element".
The syntax you have used that is not for cssSelector that for XPATH and you have missed parenthesis as well.
Try following xpath now.
WebElement big = browser.findElement(By.xpath("//div[contains(#class,'Big')]"));
WebElement medium = big.findElement(By.xpath(".//div[contains(#class,'Medium')]"));
WebElement small = medium.findElement(By.xpath(".//div[contains(#class,'Small')]"));
However you can do it in once like.
WebElement small = browser.findElement(By.xpath("//div[contains(#class,'Big')]//div[contains(#class,'Medium')]//div[contains(#class,'Small')]"));
I would like to add a few lines to the answer of #KunduK
WebElement small = browser.findElement(new ByChained(By.xpath("//div[contains(#class,'Big')]"),By.xpath("//div[contains(#class,'Medium')]"),By.xpath("//div[contains(#class,'Small')]")));
When selenium already gives a few extra implementations, then why not to use it. :-)
You can get more details from the below link:
How Selenium's ByChained class really works?
https://www.linkedin.com/pulse/selenium-classes-stabilize-ui-automation-code-durga-behera/
Brackets are missing in the locator:
WebElement big = browser.findElement(By.cssSelector("div[class*='Big']"));
WebElement medium = big.findElement(By.cssSelector("div[class*='Medium']"));
WebElement small = medium.findElement(By.cssSelector("div[class*='Small')]"));
There is syntactical errors in placing parenthesis and the locator type used.
Try below code,
WebElement big = browser.findElement(By.xpath("//div[contains(#class,'Big')]"));
WebElement medium = big.findElement(By.xpath("//div[contains(#class,'Medium')]"));
WebElement small = medium.findElement(By.xpath("//div[contains(#class,'Small')]"));
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"));
Code behind button:
<input class=”btn” id=”mypage:formid:relatedScenaiosListId:j_id27:j_id28″ name=”mypage:formid:relatedScenaiosListId:j_id27:j_id28″ onclick=”window.open(‘/apex/newscenario?Opportunity__c=006f00000072n8hAAA’,’_top’, 1);;A4J.AJAX.Submit(‘mypage:formid’,event,{‘similarityGroupingId’:’mypage:formid:relatedScenaiosListId:j_id27:j_id28′,’parameters’:{‘mypage:formid:relatedScenaiosListId:j_id27:j_id28′:’mypage:formid:relatedScenaiosListId:j_id27:j_id28′} } );return false;” value=”New” type=”button”>
I did a right click from the Inspect Element view and saw I could copy the Xpath and found it was :
//*[#id="mypage:formid:relatedScenaiosListId:j_id27:j_id28"]
Note the *.
I tired:
WebElement txtnew = driver.findElement(By.xpath(“//input[#id='mypage:formid:relatedScenaiosListId:j_id27:j_id28']“));
txtnew.click();
and
WebElement txtnew = driver.findElement(By.xpath(“//input[#id='mypage:formid:relatedScenaiosListId:j_id27:j_id28'][#value='New']“));
txtnew.click();
but neither worked.
I’m curious about the *, if that should be part of my Xpath statement?
If it's not necessary for you to use xpath, use searching by id, or cssSelectors to find your element. E.g.
//if you have only one element with class btn you can use this selector
//if element placed in parent (and this can identify element much more) add selector to
//parent before .btn
WebElement txtnew = driver.findElement(By.Css(".btn"));
//or
WebElement txtnew = driver.findElement(By.Css("input[value='New']"));
//or if id not generated automatically
WebElement txtnew = driver.findElement(By.Css("#mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
//or using By.Id
WebElement txtnew = driver.findElement(By.Id("mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
Any of them will work in specific situation. Choose one which more suitable for your sitution. Thanks.
Try this xpath:
input[#value='New'][#class='btn'][starts-with(#id, 'mypage:formid')]
The real issue, that I didn't realize, was that the control was in a different frame. Once I set the driver to look at the frame any number of xpath expressions worked.
` driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
driver.switchTo().frame("066i0000004bNpx");
WebElement txtNewbtn = driver.findElement(By.id("mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
txtNewbtn.click();`