HTML Java parsing basic error? - java

Should be a relatively easy question, but as I am a newbie to java, I dont know the answer!
I have the following code:
String FTSE = "http://www.bloomberg.com/quote/UKX:IND/members";
doc = Jsoup.connect(FTSE).get();
Elements trs = doc.select("tr:has(a[href='/quote/III:LN'])");
Elements values = trs.select("td.value");
link = values.get(0);
System.out.println("text : " + link.text());
However, there are red squiggly lines in eclipse under the word 'link' in the penultimate and final line, and when I hover over it, it says this- link cannot be resolved to a variable.
How do I fix this?
Cheers

You are trying to assign values.get(0) to a variable link, which, as Eclipse thinks, was not defined or defined elsewhere. There are two problems and two possible solutions:
You never defined link. Define it and assign it the type of what values.get returns:
SomeType link = values.get(0);
You defined link in another method. Since it's not in the same scope, you must define it for the global scope and then use it with this keyword:
this.link = values.get(0);

It seems that link is not defined. Try:
YourClass link = (YourClass)values.get(0);
Instead of YourClass use the class that values.get returns.

Related

Appium NOT locating element when java variable is used in xpath

I'm trying to locate elements dynamically usign the xpath. However, when I use variable in the xpath, elements are NOT located. However, if I use hardcoded value, elements are located properly.
What am I missing here?
Below xpath locates the elements perfectly:
driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, 'hp')]"));
whereas, below xpath doesn't locate the elements:
driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, '" + device + "')]"));
Please note that , there are multiple elements matching the above xpath.
I even tried below code but of no use:
driver.findElements(By.XPath(String.Format("//XCUIElementTypeStaticText[contains(#value, '{0}')]", device)));
Any help would be appreciated.
Try do debug this issue as following:
Define the XPath string before calling driver.findElements method, format the string to have the proper value and then pass it into Selenium method, as following:
String xpathLocator = "//XCUIElementTypeStaticText[contains(#value, '%s')]";
xpathLocator = String.format(xpathLocator, device);
driver.findElements(By.xpath(xpathLocator));
As about your existing code.
Here driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, '" + device + "')]"));
I can't see the formatting action.
And here driver.FindElements(By.XPath(string.Format("//XCUIElementTypeStaticText[contains(#value, '{0}')]", device)));
it seems to be a wrong syntax.
It should be String.format while you wrote string.Format
Try trimming the spaces as:
driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, '"+device+"')]"));
Or using String.format() as:
String device = "hp";
driver.findElements(By.xpath(String.format("//XCUIElementTypeStaticText[contains(#value, '%s')]", device)));
Note:
Instead of FindElements() it should be findElements()
Instead of String.Format() it should be String.format()
The issue was with the case mismatch in the value returned by variable. i.e; device variable was returning 'hP' instead of 'hp'.
Corrected the code and it works fine now.

Java.Selenium. How to put not constant variable inside of xpath?

I am working with Java Selenium. And I need to put String line computeEngine.getMachineSeries() from properties inside of my xpath. How can I do this?
#FindBys({#FindBy(xpath = "//md-option//div[#class='md-text ng-binding'][contains(text(), '"+computeEngine.getMachineSeries()+"')]")})
List<WebElement> seriesOptionNOne;
I cant use this code above, because of this error: "Attribute value must be constant". How can I do this in another way?
You can try the following solution:
String myString = computeEngine.getMachineSeries();
#FindBys({#FindBy(xpath = "//md-option//div[#class='md-text ng-binding'][contains(., '"+ myString +"')]")})
List<WebElement> seriesOptionNOne;
Not possible:
https://stackoverflow.com/a/10636320/1387701
There is no way to dynamically generate a string used in an
annotation. The compiler evaluates annotation metadata for
RetentionPolicy.RUNTIME annotations at compile time, but
GENERIC_GENERATED_NAME isn't known until runtime. And you can't use
generated values for annotations that are RetentionPolicy.SOURCE
because they are discarded after compile time, so those generated
values would never be known.

Java Selenium - Find string text from multiple divs with same class name using xpath

i'm hoping you can help me.
I've been going through all sorts of forums and questions on here on how to cycle through multiple divs with the same class name using xpath queries. I'm fairly new to WebDriver and Java so I'm probably not asking the question properly.
I have a table where i'm trying to identify the values within and ensure they're correct. each field has the same class identifier, and i'm able to successfully pull back the first result and confirm via report logging using the following
String className1 = driver.findElement(By.xpath("(//div[#class='table_class'])")).getText();
Reporter.log("=====Class Identified as "+className1+"=====", true);
However, when i then try and cycle through (I've seen multiple answers saying to add a [2] suffix to the xpath query) i'm getting a compile error:
String className2 = driver.findElement(By.xpath("(//div[#class='table_class'])")[2]).getText();
Reporter.log("=====Class Identified as "+className2+"=====", true);
The above gives an error saying "The type of the expression must be an array type but it resolved to By"
I'm not 100% sure how to structure this in order to set up an array and then cycle through.
Whilst this is just verifying field labels for now, ultimately i need to use this approach to verify the subsequent data that is pulled through, and i'll have the same problem then
You are getting the error for -
String className2 = driver.findElement(By.xpath("(//div[#class='table_class'])")[2]).getText();
because you are using index in wrong way modify it to -
String className2 = driver.findElement(By.xpath("(//div[#class='table_class'])[2]")).getText();
or
String className2 = driver.findElement(By.xpath("(//div[#class='table_class'][2])")).getText();
And the better way to do this is -
int index=1;
List <WebElement> allElement = driver.findElements(By.xpath("(//div[#class='table_class'])"));
for(WebElement element: allElement)
{
String className = element.getText();
Reporter.log("=====Class Identified as "+className+""+index+""+"=====", true);
index++
}

How do I get JavaScript Values from Selenium?

I am running the following code (Java selenium client)- PAGE_NUMBER has a value, but I am unable to get it using selenium:
String script = "var cellValue = selenium.browserbot.getUserWindow().PAGE_NUMBER;";
selenium.runScript(script);
String value = selenium.getEval("selenium.browserbot.getUserWindow().cellValue;");
System.out.println("Value: " + value);
I don't know Selenium 1 at all and Selenium2/Webdriver is very different. However there are three things that I suspect to play a role in this issue:
Scope: You declared the variable as local to the scope of the script (by writing var). You might try using a global variable by omitting the var keyword, so that you can access it later.
Why do you try to access the variable via selenium.browserbot.getUserWindow().. Try omitting this part.
The semicolon after cellValue is probably no good idea either
And then again, why not simply using
String value = selenium.getEval("selenium.browserbot.getUserWindow().PAGE_NUMBER");
?
I hope at least some part of this answer helps you. As I said I am just guessing.

Passing Class<?> parameter using Rhino

I am trying to call a constructor for a custom collection object. This custom object takes in a parameter of type Class.
In java, this is done like this:
ICollection col = new PersistentCollection(ContentX.class);
This is my first dive into rhino, and I haven't been able to figure out quite how to pass this parameter. I figured out that "class" is a reserved word and therefor not usable.
I figured that I could get the Class from Class.forName like this:
importPackage(Packages.something.collections);
importPackage(Packages.something.content4);
var col = new PersistentCollection(Class.forName(ContentX));
But it just tosses ClassNotFoundException - with the fully qualified path something.content4.ContentX! So obviously it found the class or it wouldn't have known the path to it.
Am I doing it wrong? Sadly, I'm not in any position to change the java library right now, I need to fix the data without a new deploy.
Googling for javascript class just yields DOM/CSS problems.
I think you simply need to do:
var col = new PersistentCollection(ContentX);
Or, if your class name is a string:
var col = new PersistentCollection(
java.lang.Class.forName('something.content4.ContentX'));

Categories

Resources