How do I get the text from the li tag - selenium-webdriver

How do I get the text from the li tag? I want to find the text "Password is required." only, not the text inside strong tag.
<li><strong>Error:</strong> Password is required.</li>

You need to show your code for somebody to give a complete answer. I guess that you already know how to do something like the following
WebElement something = driver.FindElement(By.CssSelector(?))
string s = something.Text;
The next bit seems to be where you are stuck. There you need to parse the string s. That is nothing to do with Selenium-Webdriver. You could do something like
string[] s2 = s.split(new string[] {">","<"});
were the last element in s2 would be your answer here. This would be totally non generic though. Is this a situation in which you always want to purge html?

Here is the method developed in python.
def get_text_exclude_children(element):
return driver.execute_script(
"""
var parent = arguments[0];
var child = parent.firstChild;
var textValue = "";
while(child) {
if (child.nodeType === Node.TEXT_NODE)
textValue += child.textContent;
child = child.nextSibling;
}
return textValue;""",
element).strip()
How to use in this:
liElement = driver.find_element_by_xpath("//li")
liOnlyText = get_text_exclude_children(liElement)
print(liOnlyText)
Please use your possible strategy to get the element, this method need an element from which you need the text (without children text).

Related

Emojis become question marks after re-render in contentEditable

I've created a simple text editor and I've also created the possibility to add emojis. The problem is that when I click an emoji the emoji is added and visible, but the previous existing emojis are replaced with question marks. My contentEditable div is controlled, meaning that whenever an emoji is added it causes a re-render. I got this problem in the following if case of my algorithm:
childValue = childValue.substring(0, caretPosition) + emoji + childValue.substring(caretPosition + 1, childValue.length)
childValue isn't a state variable, it's value is used to update a constant variable that will be used to update the state variable representing the contentEditable's dangerouslySetInnerHTML property, the caretPosition is perfect.
Any ideas why I might have this issue?
This solution from https://stackoverflow.com/a/59793087/14550434 combined with HTML escaping should do the trick:
var p = document.querySelector("p");
var childValue = "Some sample text";
var caretPosition = 5;
var emoji = "😀";
const emojiToString = (emoji) =>
` &#x${emoji.codePointAt(0).toString(16)}; `;
childValue =
childValue.substring(0, caretPosition) + emojiToString(emoji) +
childValue.substring(caretPosition, childValue.length);
p.innerHTML = childValue;
<p></p>
I solved this issue. I figured out that 'substring' was messing everything up, so I did the following:
newChildValue = Array.from(childValue);
childValue = newChildValue.slice(0, caretPosition).join('') + emoji + newChildValue.slice(caretPosition).join('');
This way I avoid using 'substring' as Array.from(str) splits a string into individual unicode characters without breaking them between bytes.

prepare mentions and hashtags to parse array object

this is my first post and question. Here is what i'm doing to collect hashtags from UIText Field. It's working but I think this is not the correct way to do this. Any ideas? I need an array of hashtags as words.
Need to collect these type of tags: "Hey, this is my first photo!#photo #hashtag #daily:)
Here is my code;
// Prepare words
let words:[String] = titleTxt.text!.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
self.taggyArray.removeAll(keepCapacity: false)
// define tagged word if #
for var word in words {
if word.hasPrefix("#") {
// cut symbold
word = word.stringByTrimmingCharactersInSet(NSCharacterSet.punctuationCharacterSet())
word = word.stringByTrimmingCharactersInSet(NSCharacterSet.symbolCharacterSet())
// now prepare adjacent tags
let tags:[String] = word.componentsSeparatedByCharactersInSet(NSCharacterSet.init(charactersInString: "#"))
for tag in tags {
taggyArray += [tag]
}
}
}
And save to server
object["hashtags"] = taggyArray
Is it possible to work this just with one regex seperation? Thank you,
Try this.
let matches = regex.matchesInString("(\#\w+)", ""Hey, this is my first photo!#photo #hashtag #daily:)")
println(matches)

Flash getter method is not able to return a 2D Array

As the title says. I tried it with a String or a normal Array and it works. But when I try to pass on my 2D Array my class won't get anything. We're talking about an Array 16 width and about 50 in length.
In my XMLLoader.as class I have this:
function getConvoArray():Array
{
trace("convoArray send");
return convoArray;
}
And in my DialogueGenerator.as class I have this:
xmlLoader = new XMLLoader("ConvoLines.xml");
convoArray = xmlLoader.getConvoArray();
I've checked if the variable convoArray is filled in the XMLLoader.as class by tracing it in a for loop; it works perfectly. But then, when I try to pass it on to the DialogueGenerator.as class it seems to be empty. I cannot excess anything and Flash doesn't give me an error or a warning.
I simply have my Array in DialogueGenerator declared as this:
public var convoArray:Array;
But I tried different ways of declaring it.
Is there a solution for this? A workaround?
For loading xml I use something like this....
var fileName:String = "ConvoLines.xml";
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, LoaderComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, LoaderError);
loader.load(new URLRequest(fileName + "?rnd=" + Math.random()));
// we affix rand to prevent it from caching the file,
// you don't have to add the ? variable if you aren't worried about it
// updating smoothly
Then in the "LoaderComplete" function we just get the xml out. Hope that helps.
var convoXML:XML = new XML(event.target.data);

initialized without binding with angular

I need to initialized without binding.
I've tried the following. But I did not succeed
$scope.emptyRow = $scope.newsCategories[1];
$scope.newsCategories[1].Name = "yes";
$scope.emptyRow.Name = "test";
alert($scope.emptyRow.Name); // alert => test
alert($scope.newsCategories[1].Name);// alert => test
I need this :
$scope.emptyRow = $scope.newsCategories[1];
$scope.newsCategories[1].Name = "yes";
$scope.emptyRow.Name = "test";
alert($scope.emptyRow.Name); // alert => test
alert($scope.newsCategories[1].Name);// alert => yes
How to do this?
This has nothing to do with binding, but rather basic javascript.
The line: $scope.emptyRow = $scope.newsCategories[1];
is explicitly saying that you want $scope.emptyRow and $scope.newsCategories[1] to be pointing to the exact same object. Hence, when you change a child value of either (like Name), it will effect the other.
It looks like you want to be able to copy the object in question. For that you can use angular.copy(). An example use in your case would be:
$scope.emptyRow = angular.copy($scope.newsCategories[1]);
Read here for more info.

Array Generating Images

I'm not overly sure if this is possible, as I am not a frequent programmer, but I have a question.
I've got an array that generates one random word in a text box and then a second array that generates another random word in a different text box. What I want is for when a certain word out of array number one is generated, a certain image appears with it. Here's the code:
var firstChoice:Array = ["Do this", "Do that", "Do something else"];
var secondOption:Array = ["while doing this", "while doing that", "while doing something else"];
generate_btn.addEventListener(MouseEvent.CLICK, getTask);
function getTask(event:MouseEvent):void {
var randomChoice:Number = Math.floor(Math.random() * firstChoice.length);
var randomOption:Number = Math.floor(Math.random() * secondOption.length);
Final_Choice.text = firstChoice[randomChoice];
Final_Option.text = secondOption[randomOption];
}
So for instance, when I click the button and the first array generates "Do this," I want a specific graphic to appear with it.
Hopefully this is possible :/ I'm stumped!
probably you need to use a HashMap, such as:
var map:Object = new Object();
map.first_choice = /*url of your image associated with this choice*/
map.second_choice = /*url of your image associated with this choice*/
//etc
and when a word is generated, you just compare the word with keys of the map, using a foreach, and get the url of your image

Resources