Don't understand why this while doesn't stop
I have a selenium bot doing actions on a social network. I would like it to stop after he does a certain number of actions (10 is for the example). I initialize variables this way:
def __init__(self):
self.browser = webdriver.Firefox()
self.counter_var = int(0)
self.max_var = int(10)
This is the part performing and counting actions:
def action(self, accounts):
for account in accounts[9:]:
try:
self.browser.get(account)
time.sleep(5)
like_button = self.browser.find_element_by_xpath(
u'//button[contains(@class, "Heart")]').click()
self.count_actions()
print(self.counter_var)
except selenium.common.exceptions.NoSuchElementException:
break
def count_actions(self):
self.counter_var += 1
And this is the loop I've tried to make into main:
while self.counter_var < self.max_var:
searched_category = random.choice(pool_categories)
accounts = self.load_category(searched_category)
self.action(accounts)
However the bot never stops, even when counter_var
reaches 10
.
Do you know how to correct it?
python
add a comment |
I have a selenium bot doing actions on a social network. I would like it to stop after he does a certain number of actions (10 is for the example). I initialize variables this way:
def __init__(self):
self.browser = webdriver.Firefox()
self.counter_var = int(0)
self.max_var = int(10)
This is the part performing and counting actions:
def action(self, accounts):
for account in accounts[9:]:
try:
self.browser.get(account)
time.sleep(5)
like_button = self.browser.find_element_by_xpath(
u'//button[contains(@class, "Heart")]').click()
self.count_actions()
print(self.counter_var)
except selenium.common.exceptions.NoSuchElementException:
break
def count_actions(self):
self.counter_var += 1
And this is the loop I've tried to make into main:
while self.counter_var < self.max_var:
searched_category = random.choice(pool_categories)
accounts = self.load_category(searched_category)
self.action(accounts)
However the bot never stops, even when counter_var
reaches 10
.
Do you know how to correct it?
python
you need to provide a Minimal, Complete, and Verifiable example
– juanpa.arrivillaga
Jan 1 at 20:59
Perhaps the exception you are silently catching is preventing your counter from incrementing.
– khelwood
Jan 1 at 21:05
@khelwood: probably not, since SidGabriel prints the value and reports it reaching 10.
– Yakov Dan
Jan 1 at 21:17
Do you create any more instances of this class?
– Yakov Dan
Jan 1 at 21:17
1
You can go past 10 in thefor
loop inaction()
, because you don't test against the limit untilaction()
returns to thewhile
loop.
– Barmar
Jan 1 at 21:28
add a comment |
I have a selenium bot doing actions on a social network. I would like it to stop after he does a certain number of actions (10 is for the example). I initialize variables this way:
def __init__(self):
self.browser = webdriver.Firefox()
self.counter_var = int(0)
self.max_var = int(10)
This is the part performing and counting actions:
def action(self, accounts):
for account in accounts[9:]:
try:
self.browser.get(account)
time.sleep(5)
like_button = self.browser.find_element_by_xpath(
u'//button[contains(@class, "Heart")]').click()
self.count_actions()
print(self.counter_var)
except selenium.common.exceptions.NoSuchElementException:
break
def count_actions(self):
self.counter_var += 1
And this is the loop I've tried to make into main:
while self.counter_var < self.max_var:
searched_category = random.choice(pool_categories)
accounts = self.load_category(searched_category)
self.action(accounts)
However the bot never stops, even when counter_var
reaches 10
.
Do you know how to correct it?
python
I have a selenium bot doing actions on a social network. I would like it to stop after he does a certain number of actions (10 is for the example). I initialize variables this way:
def __init__(self):
self.browser = webdriver.Firefox()
self.counter_var = int(0)
self.max_var = int(10)
This is the part performing and counting actions:
def action(self, accounts):
for account in accounts[9:]:
try:
self.browser.get(account)
time.sleep(5)
like_button = self.browser.find_element_by_xpath(
u'//button[contains(@class, "Heart")]').click()
self.count_actions()
print(self.counter_var)
except selenium.common.exceptions.NoSuchElementException:
break
def count_actions(self):
self.counter_var += 1
And this is the loop I've tried to make into main:
while self.counter_var < self.max_var:
searched_category = random.choice(pool_categories)
accounts = self.load_category(searched_category)
self.action(accounts)
However the bot never stops, even when counter_var
reaches 10
.
Do you know how to correct it?
python
python
edited Jan 1 at 23:16


martineau
69k1091186
69k1091186
asked Jan 1 at 20:58


SidGabrielSidGabriel
766
766
you need to provide a Minimal, Complete, and Verifiable example
– juanpa.arrivillaga
Jan 1 at 20:59
Perhaps the exception you are silently catching is preventing your counter from incrementing.
– khelwood
Jan 1 at 21:05
@khelwood: probably not, since SidGabriel prints the value and reports it reaching 10.
– Yakov Dan
Jan 1 at 21:17
Do you create any more instances of this class?
– Yakov Dan
Jan 1 at 21:17
1
You can go past 10 in thefor
loop inaction()
, because you don't test against the limit untilaction()
returns to thewhile
loop.
– Barmar
Jan 1 at 21:28
add a comment |
you need to provide a Minimal, Complete, and Verifiable example
– juanpa.arrivillaga
Jan 1 at 20:59
Perhaps the exception you are silently catching is preventing your counter from incrementing.
– khelwood
Jan 1 at 21:05
@khelwood: probably not, since SidGabriel prints the value and reports it reaching 10.
– Yakov Dan
Jan 1 at 21:17
Do you create any more instances of this class?
– Yakov Dan
Jan 1 at 21:17
1
You can go past 10 in thefor
loop inaction()
, because you don't test against the limit untilaction()
returns to thewhile
loop.
– Barmar
Jan 1 at 21:28
you need to provide a Minimal, Complete, and Verifiable example
– juanpa.arrivillaga
Jan 1 at 20:59
you need to provide a Minimal, Complete, and Verifiable example
– juanpa.arrivillaga
Jan 1 at 20:59
Perhaps the exception you are silently catching is preventing your counter from incrementing.
– khelwood
Jan 1 at 21:05
Perhaps the exception you are silently catching is preventing your counter from incrementing.
– khelwood
Jan 1 at 21:05
@khelwood: probably not, since SidGabriel prints the value and reports it reaching 10.
– Yakov Dan
Jan 1 at 21:17
@khelwood: probably not, since SidGabriel prints the value and reports it reaching 10.
– Yakov Dan
Jan 1 at 21:17
Do you create any more instances of this class?
– Yakov Dan
Jan 1 at 21:17
Do you create any more instances of this class?
– Yakov Dan
Jan 1 at 21:17
1
1
You can go past 10 in the
for
loop in action()
, because you don't test against the limit until action()
returns to the while
loop.– Barmar
Jan 1 at 21:28
You can go past 10 in the
for
loop in action()
, because you don't test against the limit until action()
returns to the while
loop.– Barmar
Jan 1 at 21:28
add a comment |
1 Answer
1
active
oldest
votes
Currently it's impossible to give a concrete answer to your question, due to the lack of code given, but it looks like the problem is because the action()
method doesn't check self.counter_var
while the for
loop within it is executing.
Something like the follow might work. Adding yield
to the action()
method turns it into a generator function, which makes it iterable. When that is done it will effectively "pause" at that point each iteration of its for
loop, and would allow the caller to inspect the current value of self.counter_var
(or anything else it wanted to do each iteration).
Here's what I'm suggesting with a few explanatory comments:
class Class:
def action(self, accounts):
for account in accounts[9:]:
try:
self.browser.get(account)
time.sleep(5)
like_button = self.browser.find_element_by_xpath(
u'//button[contains(@class, "Heart")]').click()
self.count_actions()
print(self.counter_var)
yield # Added.
except selenium.common.exceptions.NoSuchElementException:
break
def count_actions(self):
self.counter_var += 1
def main(self):
while True:
searched_category = random.choice(pool_categories)
accounts = self.load_category(searched_category)
for _ in self.action(accounts): # Iterate through account checks.
if self.counter_var < self.max_var: # Too many actions?
break
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998890%2fdont-understand-why-this-while-doesnt-stop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Currently it's impossible to give a concrete answer to your question, due to the lack of code given, but it looks like the problem is because the action()
method doesn't check self.counter_var
while the for
loop within it is executing.
Something like the follow might work. Adding yield
to the action()
method turns it into a generator function, which makes it iterable. When that is done it will effectively "pause" at that point each iteration of its for
loop, and would allow the caller to inspect the current value of self.counter_var
(or anything else it wanted to do each iteration).
Here's what I'm suggesting with a few explanatory comments:
class Class:
def action(self, accounts):
for account in accounts[9:]:
try:
self.browser.get(account)
time.sleep(5)
like_button = self.browser.find_element_by_xpath(
u'//button[contains(@class, "Heart")]').click()
self.count_actions()
print(self.counter_var)
yield # Added.
except selenium.common.exceptions.NoSuchElementException:
break
def count_actions(self):
self.counter_var += 1
def main(self):
while True:
searched_category = random.choice(pool_categories)
accounts = self.load_category(searched_category)
for _ in self.action(accounts): # Iterate through account checks.
if self.counter_var < self.max_var: # Too many actions?
break
add a comment |
Currently it's impossible to give a concrete answer to your question, due to the lack of code given, but it looks like the problem is because the action()
method doesn't check self.counter_var
while the for
loop within it is executing.
Something like the follow might work. Adding yield
to the action()
method turns it into a generator function, which makes it iterable. When that is done it will effectively "pause" at that point each iteration of its for
loop, and would allow the caller to inspect the current value of self.counter_var
(or anything else it wanted to do each iteration).
Here's what I'm suggesting with a few explanatory comments:
class Class:
def action(self, accounts):
for account in accounts[9:]:
try:
self.browser.get(account)
time.sleep(5)
like_button = self.browser.find_element_by_xpath(
u'//button[contains(@class, "Heart")]').click()
self.count_actions()
print(self.counter_var)
yield # Added.
except selenium.common.exceptions.NoSuchElementException:
break
def count_actions(self):
self.counter_var += 1
def main(self):
while True:
searched_category = random.choice(pool_categories)
accounts = self.load_category(searched_category)
for _ in self.action(accounts): # Iterate through account checks.
if self.counter_var < self.max_var: # Too many actions?
break
add a comment |
Currently it's impossible to give a concrete answer to your question, due to the lack of code given, but it looks like the problem is because the action()
method doesn't check self.counter_var
while the for
loop within it is executing.
Something like the follow might work. Adding yield
to the action()
method turns it into a generator function, which makes it iterable. When that is done it will effectively "pause" at that point each iteration of its for
loop, and would allow the caller to inspect the current value of self.counter_var
(or anything else it wanted to do each iteration).
Here's what I'm suggesting with a few explanatory comments:
class Class:
def action(self, accounts):
for account in accounts[9:]:
try:
self.browser.get(account)
time.sleep(5)
like_button = self.browser.find_element_by_xpath(
u'//button[contains(@class, "Heart")]').click()
self.count_actions()
print(self.counter_var)
yield # Added.
except selenium.common.exceptions.NoSuchElementException:
break
def count_actions(self):
self.counter_var += 1
def main(self):
while True:
searched_category = random.choice(pool_categories)
accounts = self.load_category(searched_category)
for _ in self.action(accounts): # Iterate through account checks.
if self.counter_var < self.max_var: # Too many actions?
break
Currently it's impossible to give a concrete answer to your question, due to the lack of code given, but it looks like the problem is because the action()
method doesn't check self.counter_var
while the for
loop within it is executing.
Something like the follow might work. Adding yield
to the action()
method turns it into a generator function, which makes it iterable. When that is done it will effectively "pause" at that point each iteration of its for
loop, and would allow the caller to inspect the current value of self.counter_var
(or anything else it wanted to do each iteration).
Here's what I'm suggesting with a few explanatory comments:
class Class:
def action(self, accounts):
for account in accounts[9:]:
try:
self.browser.get(account)
time.sleep(5)
like_button = self.browser.find_element_by_xpath(
u'//button[contains(@class, "Heart")]').click()
self.count_actions()
print(self.counter_var)
yield # Added.
except selenium.common.exceptions.NoSuchElementException:
break
def count_actions(self):
self.counter_var += 1
def main(self):
while True:
searched_category = random.choice(pool_categories)
accounts = self.load_category(searched_category)
for _ in self.action(accounts): # Iterate through account checks.
if self.counter_var < self.max_var: # Too many actions?
break
edited Jan 1 at 23:46
answered Jan 1 at 23:41


martineaumartineau
69k1091186
69k1091186
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998890%2fdont-understand-why-this-while-doesnt-stop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you need to provide a Minimal, Complete, and Verifiable example
– juanpa.arrivillaga
Jan 1 at 20:59
Perhaps the exception you are silently catching is preventing your counter from incrementing.
– khelwood
Jan 1 at 21:05
@khelwood: probably not, since SidGabriel prints the value and reports it reaching 10.
– Yakov Dan
Jan 1 at 21:17
Do you create any more instances of this class?
– Yakov Dan
Jan 1 at 21:17
1
You can go past 10 in the
for
loop inaction()
, because you don't test against the limit untilaction()
returns to thewhile
loop.– Barmar
Jan 1 at 21:28