Creating Highlight in Selenium Webdriver - selenium-webdriver

I need to select a group of text in a web page by clicking the left click of the mouse. When I release the left click, one menu apppears. How to select the group of text in Selenium?

Try this:
public class SelectText {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.co.in");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.xpath("html/body/div[1]/div[5]/span/center/div[3]/div/div"));
Actions actions = new Actions(driver);
actions.moveToElement(element, 0, 0)
.clickAndHold()
.moveByOffset(50, 0)
.release()
.perform();
}
}
1)Here I am trying to select Google text that is below the search button
2)I am finding the element and then moving to the element at position (0,0)which is the top left corner of the element,
3)By using click and hold I am moving to the right using moveByOffset function by 50, you can vary x and y parameters of moveByOffset function based on your text selection area.
Hope it helps

Related

How to get userdefine WebElement name in page object model to print in report log

I need to get the WebElement name(Userdefine name) for reporting Purpose. Performing Click operation on AddMainConcernLink : If The element is Clicked/not .I need to report "AddMainConcernLink" is Clicked/not found
[FindsBy(How = How.CssSelector, Using = "[data-test-id='ECNMainConcernsLink']")]
private IWebElement AddMainConcernLink;
public void Click(IWebElement element)
{
element.Click();
Console.WriteLine("Perfomed click operation on element : " + element);
}
I want to Print Perfomed click operation on Element: AddMainConcernLink.
I simply pass a descriptive parameter along with the web element to my helper routines. For example, in a calculator Android app, I have the following:
public void clickDegreeRadsToggle() {
helper.click(degreeRadsToggle, "Degree/Rads Toggle");
}
And the helper.click method then logs using the passed description.

Selenium WebDriver issues with Material UI select fields (dropdown list)

Hi I need to access to a Select Field component from the Material UI library. I'm using the traditional way but as expected is throwing an error because this library generates div elements instead of select.
Please any idea of how to select elements with this component?
WebSite Url: http://www.material-ui.com/#/components/select-field
Error: "org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "div".
The code I used is the follow:
public class MaterialUITest {
private WebDriver driver;
By selectFieldLocator = By.xpath("//div[contains(#id,'undefined-undefined-Frequency')]/div[1]/div[2]");
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "./src/test/resources/drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.material-ui.com/#/components/select-field");
}
#After
public void tearDown() throws Exception {
}
#Test
public void test() {
WebElement selectField = driver.findElement(selectFieldLocator);
Select dropdown = new Select(selectField);
dropdown.selectByVisibleText("Weekly");
WebElement option = dropdown.getFirstSelectedOption();
System.out.println(option.getText());
}
}
As I understand problem that you're having is not related to material itself it's more related to custom implementation of select, i.e. this is not select, you should treat this 'select' as regular web element and handle it respectively in other words you need to click on it to expand and then perform another click on required element to select it.
Inspect element and copy full xpath.
Try with Full xpath it will work you need take the xpath of full tab because inner elements are not enabled because of parent elements.

Selecting the link from multiple search results in selenium webdriver

I am writing the test code using java selenium web driver,
I want to search for "Honey rose varghese" and when the result is available, from this result I want to click the Filmography link available on the result page.
public class ByPartialLink {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.co.in");
WebElement searchbox = driver.findElement(By.id("lst-ib"));
searchbox.sendKeys("Honey rose varghese");
searchbox.submit();
Thread.sleep(3000);
List<WebElement> search = driver.findElements(By.partialLinkText("Filmography"));
System.out.println(search.size());
}
}
This worked for me and returned 1. I just searched for the full link text instead of partial. Using a Thread.sleep() isn't a best practice. Try the WebDriverWait below for better, more consistent results.
driver.get("http://www.google.co.in");
WebElement searchbox = driver.findElement(By.id("lst-ib"));
searchbox.sendKeys("Honey rose varghese");
searchbox.submit();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));
// Thread.sleep(3000);
List<WebElement> search = driver.findElements(By.linkText("Filmography"));
System.out.println(search.size());
EDIT 1: after feedback
If you want to click the first Filmography link, there are a couple ways to do this...
1) Maybe you want to count the number of links to make sure there's only one, etc. You could still use the code above but click the first element.
List<WebElement> search = driver.findElements(By.linkText("Filmography"));
search.get(0).click();
2) If you always want to click the first link or are confident that there will ever only be one link you can just use .findElement() to return only the first element and click it.
WebElement search = driver.findElement(By.linkText("Filmography"));
search.click();
or a one-liner...
driver.findElement(By.linkText("Filmography")).click();
Best practice in a case like this (where there might be none or more than one search result) would be to return the collection and make sure there is at least one.
List<WebElement> search = driver.findElements(By.linkText("Filmography"));
if (!search.isEmpty())
{
search.get(0).click();
}

Unity UI Slider to change UI Text

Ok Im using unity 4.6 and have a UI slider visible to the player. Beside the slider I have a UI Text which is showing a String of 500. The Slider head is at the right hand side (500) and can be slid left towards what I want to be 0.
I have the following code in place but when i slide the slider the Text value starts at 500 and automatically changes to 0.99 and moves the whole way down to 0 on the left hand side. If i was to slide it back up to the right hand side it has a value of 1.
public class SliderUITextUpdate : MonoBehaviour {
string sliderTextString = "500";
public Text sliderText;
public void textUpdate(float textUpdateNumber)
{
sliderTextString = textUpdateNumber.ToString ();
sliderText.text = sliderTextString;
}
}
This script is attached to a Manager object (just an empty gameobject) and then the text game object is applied in the inspector.
On the slider object I am using the OnValueChanged() ability in the inspector. To which I have attached the Manager gameobject and set it to use SliderUITexture.textUpdate
Any help would be great thanks !
public class SliderUITextUpdate : MonoBehaviour {
string sliderTextString = "500";
public Text sliderText;
public Slider mainSlider;
void Awake(){
mainSlider.maxValue = 500;
}
public void textUpdate(float textUpdateNumber)
{
sliderTextString = textUpdateNumber.ToString ();
sliderText.text = sliderTextString;
}
}
get the referece for that slider in inspector

two elements of same class in selenium webdriver

I am working on selenium web driver using the language "Java" and want to access two elements of same classname. Actually, both the elements are error messages which are coming in small popup having the same class. But the problem is that every time it only picks the first element of the class which is coming. Please suggest which method I should use to get both the elements.
Also, I need to compare both the messages with the string that I have added. Here is the code I have tried:
public class mysignup {
public static WebDriver d;
public static void main(String []args)throws Exception{
d = new FirefoxDriver();
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
d.findElement(By.name("firstname")).sendKeys("qwertyuiokjhgfdsazxcvbnmkloiuytr");
d.findElement(By.name("firstname")).click();
d.findElement(By.name("lastname")).sendKeys("singh");
d.findElement(By.name("email_id")).sendKeys("abcgmail.com");
d.findElement(By.name("firstname")).click();
d.findElement(By.name("email_id")).click();
String bodyText = d.findElement(By.cssSelector(".popover-content")).getText();
While findElement returns you a single WebElement, findElements will return all elements that match given conditions.
In such a scenario I would suggest using findElements method. It will return you a list of all elements if found or an empty list. So you can try out with:
List<WebElement> lstEle = d.findElements(By.cssSelector(".popover-content"));
List<String> strLst = new ArrayList<String>();// list to contain all texts in each element
lst.forEach(new Consumer<WebElement>() { // foreach element add text to strLst
#Override
public void accept(WebElement t) {
strLst.add(t.getText());
}
});

Resources