Hey everyone
I am 15 years old and I'm currently making a python/selenium script that automatically loges in on my school page and goes in to see what homework i have tomorrow.
The way I want it to check if we have received homework is if the text box which is: "LB MAT 86" is something else (because that means we have received homework)
Let me now tell you about my problem:
Picture: https://i.stack.imgur.com/WLjuR.png
But first, let me tell u how my "homework page" is designed and works.
On the picture you see that I have inspected the text: "LB MAT 86".
LB is the abbreviation of my teachers name.
MAT stands for "matematik" with just means math in english.
86 is the class number our class have.
When we get homework our teacher deletes the text "LB MAT 86" and instead writes the homework we get.
My question for you is how can I find the text. The only way I can see how you can find the text is by its location.
The xpath of the text field: //*[#id="sk-diary-notes-container"]/div/div[1]/div/table/tbody/tr[3]/td[3]/text()[1]
The full xpath of the text field: /html/body/div[1]/div[2]/div[3]/div[2]/div/div/div[1]/div/table/tbody/tr[3]/td[3]/text()[1]
OuterHTML: LB MAT 86
Thanks
driver.find_element_by_xpath("//table[contains(#align,'center')//td[contains(.,'LB MAT 86')]")
Would get the element with text LB MAT 86.
You can add some code to find the current index to check for your code. /tr[i]/td[i]
Try using this xpath
locator = driver.find_element_by_xpath("//table[contains(#align,'center')/tbody/tr[3]/td[3]")
You should get everything including nbsp (non-braking space).
Then split the result and get everything after nbsp;
You did not include html code, only screenshot. Next time include it (or include it now).
After you test the correct locator, try to use split(). It will get the last element from your text divided by space. I am not 100% sure it will work, experiment by yourself.
mystring = locator.text
desired_text = result = mystring.split(' ')[-1]
Related
I'm writing a script to scrape from another website with Python, and I am facing this question that I have yet to figure out a method to resolve it.
So say I have set to replace this particular string with something else.
word_replace_1 = 'dv'
namelist = soup.title.string.replace(word_replace_1,'11dv')
The script works fine, when the titles are dv234,dv123 etc.
The output will be 11dv234, 11dv123.
However if the titles are, dv234, mixed with dvab123, even though I did not set dvab to be replaced with anything, the script is going to replace it to 11dvab123. What should I do here?
Also, if the title is a combination of alphabits,numbers and Korean characters, say DAV123ㄱㄴㄷ,
how exactly should I make it to only spitting out DAV123, and adding - in between alphabits and numbers?
Python - making a function that would add "-" between letters
This gives me the idea to add - in between all characters, but is there a method to add - between character and number?
the only way atm I can think of is creating a table of replacing them, for example something like this
word_replace_3 = 'a1'
word_replace_4 = 'a2'
.......
and then print them out as
namelist3 = soup.title.string.replace(word_replace_3,'a-1').replace(word_replace_4,'a-2')
This is just slow and not efficient. What would be the best method to resolve this?
Thanks.
ok i was wondring what is the best way to attempt to read a whole text file for some text if it finds the text it changes the text inside the "" i know this can be done but i never really looked into this or had to do this before and im not sure how to approach it. this is the stuff im looking for.
this is the sort of code i want to change
add_weapon( "b23r" );
and it finds add_weapon ("replace whats in here....");
because i have a program when i hit the check box i want to make it so if i copy over a pistol i can make it the start weapon but to do that i need to replace the b23r with the new weapon name when i push the check box and hit copy weapons i want it to replace that name with the weapon copying over.
so for example
if (zombie_colt == true )
{
add_weapon( "zombie_colt" );
}
something along those lines because i want it to only work for pistols i just dont know the best way to read tho a text file look for that find the quotes and change whats inside those
i looked into regex but it really confuses me and was asking here to see if anyone knows of a better way of accomplishing this
thanks in advance elfenliedtopfan5
Your question is quite vague but if I understand what you're looking for - you want to replace all instances of some text (in a text file) with a different value.
Because you were talking about regex, here is a short example to get you started for doing this using regex:
const string FILENAME = <path to your file>;
string data = File.ReadAllText(FILENAME);
Regex r = new Regex(#"add_weapon\( ""[0-9A-Za-z_]+"" \);");
string s = r.Replace(data, "add_weapon( \"REPLACE_HERE\" );");
File.WriteAllText(FILENAME, s);
Replace the REPLACE_HERE in line 4 with whatever you need.
In addition - I recommend reading more about Regex.Replace
This is my code:
for films in filmlist:
with codecs.open('peliculas.txt', encoding='utf8', mode='r') as lfile:
filmsDone = lfile.read()
filmsDoneList = filmsDone.split(',')
if films not in filmsDoneList:
with codecs.open('peliculas.txt', encoding='utf8', mode='a+') as lfile:
lfile.write(films.strip() + ',')
It will never recognize the last item of the list.
I have printed filmsDoneList and the last item in PyCharm looks like this: u'X Men.Primera Generacion'. I have printed films and they looks like this: X Men.Primera Generacion'
So I have no idea where is the problem. Thanks in advance.
#Rafa, for you to better understand what I meant in the comments, I had to write an entire answer in order for me to attach codes and screenshots.
Let's say the peliculas.txt file has the following format:
You can import such file in python according the following 3 commands:
fileIN=open('peliculas.txt','r')
filmsDoneList=fileIN.readlines()
fileIN.close()
So you basically open the file, import each line thanks to readlines() and then close the file because its contents are available in filmsDoneList. The latter has the following contents (in PyCharm):
Obviously this list is quite long and does not fit in my screen, but you get the point.
You can now get rid of that annoying newline tag '\r\n' by means of the following loop:
for id in range(len(filmsDoneList)):
filmsDoneList[id]=filmsDoneList[id].strip()
and now filmsDoneList has the form:
much better now, innit?
Now, let's say you want to add the following films:
newFilms=['The Exorcist','Back to the Future','Aliens','Back to the Future']
To make your code more robust, I have added Back to the Future twice. Basically you can get rid of duplicates in newFilms by means of the set() function. This will convert newFilms in a set with duplicates removed, but we will convert it back to a list thanks to this command:
newFilms=list(set(newFilms))
and now newFilms has the form:
Now that everything has been sorted, it's time to check if items in newFilms already are in filmsDoneList which, recall, is the contents of peliculas.txt.
Reopen peliculas.txt as follows:
fileOUT=open('peliculas.txt','a')
the 'a' tag means "append", so basically everything you write will be added to the file without removing anything from it.
And the main loop goes:
for film in newFilms:
if film in filmsDoneList:
pass
else:
fileOUT.write(film+'\n')
the pass means "do nothing". The write commands also appends the newline tag to the movie title: this will keep the previous format of 1 title per line. At the end of this loop you might as well close fileOUT.
The resulting peliculas.txt is
and, as you can see, Back to the Future was in newFilms but wasn't appended to the end of this file because already was in it. As instead, The Exorcist and Aliens have been appended to this file, at the bottom.
If your file has titles separated by commas, this approach is still valid. However you must add
filmsDoneList=filmsDoneList[0].split(',')
after the first for loop. Also in the write function (in the last for loop) you might want to replace the newline value with a comma.
This approach is cleaner, I reckon will also fix the problem you've been having and avoids continuous open/close files in a loop. Hope this helps!
i´ve got small problem and before I spend even more time in trying to solve it i´d like to know if what I want to do is even possible ( and maybe input on how to do it^^).
My problem:
I want to take some text and then split it into different strings at every whitespace (for example "Hello my name is whatever" into "Hello" "my" "name" "is" "whatever").
Then I want to set every string with it´s own variable so that I get something alike to a= "Hello" b= "my" and so on. Then I want to compare the strings with other strings (the idea is to get addresses from applications without having to search through them so I thought I could copy a telephone book to define names and so on) and set matching input to variables like Firstname , LastName and street.
Then, and here comes the "I´d like to know if it´s possible" part I want it to put it into our database, this means I want it to copy the string into a text field and then to go to the next field via tab. I´ve done something like this before with AutoIT but i´ve got no idea how to tell AutoIT whats inside the strings so I guess it must be done through the programm itself.
I´ve got a little bit of experience with c++, python and BATCH files so it would be nice if anyone could tell me if this can even be done using those languages (and I fear C++ can do it and I´m just to stupid to do so).
Thanks in advance.
Splitting a string is very simple, there is usually a built in method called .split() which will help you, the method varies from language to language.
When you've done a split, it will be assigned to an array, you can then use an index to get the variables, for example you'd have:
var str = "Hello, my name is Bob";
var split = str.split(" ");
print split[0]; // is "Hello,"
print split[1]; // is "my" etc
You can also use JSON to return data so you could have an output like
print split["LastName"];
What you're asking for is defiantly possible.
Some links that could be useful:
Split a string in C++?
https://code.google.com/p/cpp-json/
Experimenting with Mathjax on my site, I face a problem when I type in
$1<x<2$
The outcome will be as follows
This, for example, has no issues.
$x<1\text{ or }x>2$
How do I make the first one display normally?
I have attached the issue at http://teach.sg/mathematics/additional-mathematics/mathjax/.
Since < is used to start a tag in HTML, the browser considers 1<x to be a 1 followed by a tag beginning <x, and everything up to the next > becomes part of that tag. This happens long before MathJax has a chance to look for mathematics on the page, so MathJax is not able to process this math as you intended it.
You have already identified one solution (using \lt and \gt) You can also just use spaces in most cases: $1 < x < 2$.
Instead of using $1<x<2$, use $1\lt x \lt 2$.