Python Selenium Multithread Add Values From Each Thread Together - python

First of all idk if this is a duplicate question. I'm sorry if it is. But here is my problem.
I'm multi-threading and I want to add 2 values from each thread together. Here is my code for reference
def clicks():
driver = webdriver.Chrome("C:\\chromedriver.exe")
driver.get(#url)
place = #element of the place i want to click
count = 0
while True:
place.click()
count+=1
print(f'Clicks: {count}')
#for every click count + 1 and then prints the number of count
The above code works perfectly for single thread. But when I multithread it, this is the output :
Clicks: 50
Clicks: 102
I want to add the count from each thread together, I can't figure out a solution to do that. Would appreciate for any help, Thanks in advance.
Here is the full code: https://pastebin.com/Nm2dC74B

Related

How to streamline while loops for infinite cycle with pyautogui

I've lurked on SO for a little while, but all of the existing posts I've seen haven't been able to help me. I'm currently teaching myself python, so I apologize if this is an easy fix I'm not seeing.
My objective with this piece of code is to cycle through tabs in a browser using pyautogui.hotkey. It takes user input for number of tabs to cycle through, and executes the pyautogui command.
My issue, however, is that I can't manage to create a looping for or while loop.
I've played around with should_restart variables, for i in range(x) etc etc, but I'm just not seeing my fix.
The code below is essentially what I want to streamline together.
My idealized flow is:
Take input -> increase tabcounter by 1 until it equals input -> reset tabcounter-> rinse & repeat.
numberofTabs = input('How many tabs do you have? \n')
tabcounter = 0
while int(tabcounter) < int(numberofTabs):
tabcounter = tabcounter+1
pyautogui.hotkey('alt', str(tabcounter))
break
while int(tabcounter) == int(numberofTabs):
tabcounter = 0
I want this code to loop until I interrupt it, or for a lengthy period of time.
Thank you in advance. I appreciate the help!
EDIT: After reworking it, and wrapping my code in a loop, I came up with this:
loopcount = input('How many times do you want this to loop?')
time.sleep(5)
count = 0
for i in range(int(loopcount)):
while count < int(numberofTabs):
count += 1
pyautogui.hotkey('alt', str(count))
time.sleep(1)
else:
count = 0```

can't stop while loop going to the top of code in python

I have written a code in which i am using while loop.Inside this while loop I am executing a SOLR database query and until I get the result I want it to be inside the while loop.
Here is small part of the code.
def main_code():
START_OF_THE_CODE
count = 0
while True:
get_data = requests.get(solr_data)
count += 1
if get_data.status_code == 200 or count == 10:
break
REMANING CODE
The problem here is, when the query is not successful the first time, it checks the while condition and executes the query once again and during the execution it directly jumps to the START_OF_THE_CODE and starts executing the entire code again. I don't know what i am doing wrong.
Any help is appreciated.

Python and Selenium: Automatically adjusting range depending on if something exists

The first post on Stackoverflow, this website has been very useful to me in the past so wanted to thank the community first and foremost. I've been learning Python over the last 2-3 weeks, just by doing my own little "projects", and I had a question that I tried searching for but was not really sure on how to phrase it so finding an answer is a bit difficult.
Essentially what I would like to do is take a user input for a Pokémon, go to a website which has information on it and prints a table of the moves that Pokémon can learn by leveling up. I've managed to get a code running, however, the issue I have is that each Pokémon learns a different number of moves. The code I have is:
import selenium.webdriver as webdriver
def moves(x):
move = browser.find_element_by_xpath("""//*[#id="svtabs_moves_15"]/div[1]/div[1]/div[1]/table/tbody/tr[""" + str(x) + """]/td[2]/a""").text
return(move)
poke = input("Search for which Pokémon?: ")
browser = webdriver.PhantomJS()
browser.get("https://pokemondb.net/pokedex/" + str(poke))
for x in range(1,50):
print(moves(x))
If a Pokémon only has 15 moves it learns by level up, then on the 16th iteration of x, an error is returned because that xpath doesn't exist, so I am looking for a way to modify my code such that it stops printing if the xpath doesn't exist.
I was thinking using a while True statement, but not too sure how to approach it. Again, I'm very new to Python so the code may not be the most elegant.
Thanks for reading!
Using while loop with a try except statement. So if the element is not present it just stops the loop.
def moves(x):
while True:
try:
move = browser.find_element_by_xpath("""//*[#id="svtabs_moves_15"]/div[1]/div[1]/div[1]/table/tbody/tr[""" + str(x) + """]/td[2]/a""").text
except:
break
return(move)
easiest option here is to add try-except to pass error without breaking loop:
...
(your code above)
while 1: #initially was for x in range(1,50), but while loop is better
try:
print(moves(x))
except:
break
If the difference between each move is in the <tr> tag you can locate list of all those elements and use it to get the data you are looking for
def moves(element):
move = element.find_element_by_xpath('//*td[2]/a').text
return(move)
browser.get("https://pokemondb.net/pokedex/" + str(poke))
moves_list = browser.find_element_by_xpath("""//*[#id="svtabs_moves_15"]/div[1]/div[1]/div[1]/table/tbody/tr[""" + str(x) + """]""")
for x in range(1, len(moves_list)):
print(moves(moves_list[x]))

while loop and method calls while loop iteration pause until methofs finish python

I have a while loop below the commented out portions are from a previous test.
My hope was that the while loop would execute the methods from top to bottom and as the methods finish the methods below would start.
My hope felt valid as when the code was not in a while loop that was what was happening. Or maybe it just seemed that way?
My point is the code is executing in such a way that my assumption above seems wrong.
can any one please explain this to me?
counter = (len(cities)-1)
count = 0
while count != counter:
setCity(cities[count])
getApiData()
#populateVars()
#storeInDatabase()
#goToNextPage()
count +=1
storeData.createCSV(OTS.CSVname)
storeData.purgeDatabase()
Just iterate over the array without an index:
for city in cities:
setCity(city)
getApiData()
It seems that the code is indeed being run sequentially, unless there's some concurrency behind the scenes that you don't know about or aren't mentioning.
Regarding your code as it is, I think you're mistakenly subtracting 1 from len(cities); and anyhow, you should just iterate directly over the cities using a for loop.
for city in cities:
setCity(city)
getApiData()
# populateVars()
# storeInDatabase()
# goToNextPage()
storeData.createCSV(OTS.CSVname)
storeData.purgeDatabase()
I will update this answer if there are any changes/clarifications.

Python: Accept user input at any time

I am creating a unit that will do a number of things, one of them counting cycles of a machine. While I will be transferring this over to ladder logic (CoDeSys), I am putting my ideas into Python first.
I will have a count running, with just a simple
counter += 1
print("counter")
to keep track of what cycle I'm on. However, I want to be able to reset this count at any time, preferably by typing "RESET" I understand how to use the input command,
check = input()
however, I do not know how to let the program run while it is searching for an input, or whether or not this is possible at all. Thank you in advance for any answer.
If it helps to understand, here is the code. The big gap is where the problem is. http://pastebin.com/TZDsa4U4
If you only want to signal a reset of the counter, you can catch KeyboardInterrupt exception.
while True:
counter = 0
try:
while True:
counter += 1
print("counter")
except KeyboardInterrupt:
pass

Categories

Resources