Python, Selenium find element with class and wait for class change
I have a web page which loads content dynamically and while page loads, there is spinning wheel, I already found solution to grab content loaded immediately on page, but seems i can't find solution to grab content loaded later in dom.
What i can think of is to find element with specific class of that wheel spinning, and wait for it to change, once it's changed, than it means content is loaded in dom.
I am using Selenium
with Firefox
webdriver
on Ubuntu
.
Here is the class i am looking to monitor:
<div class="wheel spinning"></div>
Once content is loaded, wheel stop spinning and class is changed to:
<div class="wheel"></div>
Anyone find solution to find and monitor class="wheel spinning"
and once it's changed to class="wheel"
to continue to grab data.
Edit:
The XPATH actually solved one part of solution, here's part of code
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))
)
title = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]')
print(title.text)
But if element don't appear within 10 seconds it error's out, now to find a way to retry again and again until element is present on page.
Is there a difference in use presence_of_element_located((By.XPATH))
and find_element_by_xpath
python selenium selenium-webdriver css-selectors webdriverwait
|
show 1 more comment
I have a web page which loads content dynamically and while page loads, there is spinning wheel, I already found solution to grab content loaded immediately on page, but seems i can't find solution to grab content loaded later in dom.
What i can think of is to find element with specific class of that wheel spinning, and wait for it to change, once it's changed, than it means content is loaded in dom.
I am using Selenium
with Firefox
webdriver
on Ubuntu
.
Here is the class i am looking to monitor:
<div class="wheel spinning"></div>
Once content is loaded, wheel stop spinning and class is changed to:
<div class="wheel"></div>
Anyone find solution to find and monitor class="wheel spinning"
and once it's changed to class="wheel"
to continue to grab data.
Edit:
The XPATH actually solved one part of solution, here's part of code
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))
)
title = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]')
print(title.text)
But if element don't appear within 10 seconds it error's out, now to find a way to retry again and again until element is present on page.
Is there a difference in use presence_of_element_located((By.XPATH))
and find_element_by_xpath
python selenium selenium-webdriver css-selectors webdriverwait
1
I think you might be able to use a variant of what's here: stackoverflow.com/a/26567563/142780, specifically where they wait for a particular item (in that case found via an id rather than a class): WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
– Neil
Nov 10 '18 at 15:03
I tried this, but problem is ID always change on page, every time page refresh id of element get different, while class stays the same. I am looking also at css_selector options and xpath but none of them working for my tests.
– Aleksandar Đorđević
Nov 10 '18 at 15:07
1
But isn't there a By.CLASS_NAME? I think I'd best leave it to you, but I'm sure it's possible as did this kind of thing myself (just was a long time ago and don't have code to hand to give you the exact right syntax). NB: if you're using XPATH there are lots of simple slip ups it's easy to make, so it can be handy to test your XPATH in the Console first
– Neil
Nov 10 '18 at 15:16
1
I've just seen your XPATH edit. Does WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))) not work? Or is it matching when both classes are present? (ie wheel and spinning). I'm thinking you can skip monitoring the 'wheel spinning' version and simply wait until the appearance of a class with just 'wheel'.
– Neil
Nov 10 '18 at 15:58
1
You are right, this really do the trick. it wasn't working with CLASS_NAME or CSS_SELECTOR but XPATH looks like it's looking for exact class.
– Aleksandar Đorđević
Nov 10 '18 at 16:23
|
show 1 more comment
I have a web page which loads content dynamically and while page loads, there is spinning wheel, I already found solution to grab content loaded immediately on page, but seems i can't find solution to grab content loaded later in dom.
What i can think of is to find element with specific class of that wheel spinning, and wait for it to change, once it's changed, than it means content is loaded in dom.
I am using Selenium
with Firefox
webdriver
on Ubuntu
.
Here is the class i am looking to monitor:
<div class="wheel spinning"></div>
Once content is loaded, wheel stop spinning and class is changed to:
<div class="wheel"></div>
Anyone find solution to find and monitor class="wheel spinning"
and once it's changed to class="wheel"
to continue to grab data.
Edit:
The XPATH actually solved one part of solution, here's part of code
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))
)
title = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]')
print(title.text)
But if element don't appear within 10 seconds it error's out, now to find a way to retry again and again until element is present on page.
Is there a difference in use presence_of_element_located((By.XPATH))
and find_element_by_xpath
python selenium selenium-webdriver css-selectors webdriverwait
I have a web page which loads content dynamically and while page loads, there is spinning wheel, I already found solution to grab content loaded immediately on page, but seems i can't find solution to grab content loaded later in dom.
What i can think of is to find element with specific class of that wheel spinning, and wait for it to change, once it's changed, than it means content is loaded in dom.
I am using Selenium
with Firefox
webdriver
on Ubuntu
.
Here is the class i am looking to monitor:
<div class="wheel spinning"></div>
Once content is loaded, wheel stop spinning and class is changed to:
<div class="wheel"></div>
Anyone find solution to find and monitor class="wheel spinning"
and once it's changed to class="wheel"
to continue to grab data.
Edit:
The XPATH actually solved one part of solution, here's part of code
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))
)
title = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]')
print(title.text)
But if element don't appear within 10 seconds it error's out, now to find a way to retry again and again until element is present on page.
Is there a difference in use presence_of_element_located((By.XPATH))
and find_element_by_xpath
python selenium selenium-webdriver css-selectors webdriverwait
python selenium selenium-webdriver css-selectors webdriverwait
edited Nov 12 '18 at 12:23
DebanjanB
39.1k73576
39.1k73576
asked Nov 10 '18 at 14:57
Aleksandar ĐorđevićAleksandar Đorđević
61051545
61051545
1
I think you might be able to use a variant of what's here: stackoverflow.com/a/26567563/142780, specifically where they wait for a particular item (in that case found via an id rather than a class): WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
– Neil
Nov 10 '18 at 15:03
I tried this, but problem is ID always change on page, every time page refresh id of element get different, while class stays the same. I am looking also at css_selector options and xpath but none of them working for my tests.
– Aleksandar Đorđević
Nov 10 '18 at 15:07
1
But isn't there a By.CLASS_NAME? I think I'd best leave it to you, but I'm sure it's possible as did this kind of thing myself (just was a long time ago and don't have code to hand to give you the exact right syntax). NB: if you're using XPATH there are lots of simple slip ups it's easy to make, so it can be handy to test your XPATH in the Console first
– Neil
Nov 10 '18 at 15:16
1
I've just seen your XPATH edit. Does WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))) not work? Or is it matching when both classes are present? (ie wheel and spinning). I'm thinking you can skip monitoring the 'wheel spinning' version and simply wait until the appearance of a class with just 'wheel'.
– Neil
Nov 10 '18 at 15:58
1
You are right, this really do the trick. it wasn't working with CLASS_NAME or CSS_SELECTOR but XPATH looks like it's looking for exact class.
– Aleksandar Đorđević
Nov 10 '18 at 16:23
|
show 1 more comment
1
I think you might be able to use a variant of what's here: stackoverflow.com/a/26567563/142780, specifically where they wait for a particular item (in that case found via an id rather than a class): WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
– Neil
Nov 10 '18 at 15:03
I tried this, but problem is ID always change on page, every time page refresh id of element get different, while class stays the same. I am looking also at css_selector options and xpath but none of them working for my tests.
– Aleksandar Đorđević
Nov 10 '18 at 15:07
1
But isn't there a By.CLASS_NAME? I think I'd best leave it to you, but I'm sure it's possible as did this kind of thing myself (just was a long time ago and don't have code to hand to give you the exact right syntax). NB: if you're using XPATH there are lots of simple slip ups it's easy to make, so it can be handy to test your XPATH in the Console first
– Neil
Nov 10 '18 at 15:16
1
I've just seen your XPATH edit. Does WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))) not work? Or is it matching when both classes are present? (ie wheel and spinning). I'm thinking you can skip monitoring the 'wheel spinning' version and simply wait until the appearance of a class with just 'wheel'.
– Neil
Nov 10 '18 at 15:58
1
You are right, this really do the trick. it wasn't working with CLASS_NAME or CSS_SELECTOR but XPATH looks like it's looking for exact class.
– Aleksandar Đorđević
Nov 10 '18 at 16:23
1
1
I think you might be able to use a variant of what's here: stackoverflow.com/a/26567563/142780, specifically where they wait for a particular item (in that case found via an id rather than a class): WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
– Neil
Nov 10 '18 at 15:03
I think you might be able to use a variant of what's here: stackoverflow.com/a/26567563/142780, specifically where they wait for a particular item (in that case found via an id rather than a class): WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
– Neil
Nov 10 '18 at 15:03
I tried this, but problem is ID always change on page, every time page refresh id of element get different, while class stays the same. I am looking also at css_selector options and xpath but none of them working for my tests.
– Aleksandar Đorđević
Nov 10 '18 at 15:07
I tried this, but problem is ID always change on page, every time page refresh id of element get different, while class stays the same. I am looking also at css_selector options and xpath but none of them working for my tests.
– Aleksandar Đorđević
Nov 10 '18 at 15:07
1
1
But isn't there a By.CLASS_NAME? I think I'd best leave it to you, but I'm sure it's possible as did this kind of thing myself (just was a long time ago and don't have code to hand to give you the exact right syntax). NB: if you're using XPATH there are lots of simple slip ups it's easy to make, so it can be handy to test your XPATH in the Console first
– Neil
Nov 10 '18 at 15:16
But isn't there a By.CLASS_NAME? I think I'd best leave it to you, but I'm sure it's possible as did this kind of thing myself (just was a long time ago and don't have code to hand to give you the exact right syntax). NB: if you're using XPATH there are lots of simple slip ups it's easy to make, so it can be handy to test your XPATH in the Console first
– Neil
Nov 10 '18 at 15:16
1
1
I've just seen your XPATH edit. Does WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))) not work? Or is it matching when both classes are present? (ie wheel and spinning). I'm thinking you can skip monitoring the 'wheel spinning' version and simply wait until the appearance of a class with just 'wheel'.
– Neil
Nov 10 '18 at 15:58
I've just seen your XPATH edit. Does WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))) not work? Or is it matching when both classes are present? (ie wheel and spinning). I'm thinking you can skip monitoring the 'wheel spinning' version and simply wait until the appearance of a class with just 'wheel'.
– Neil
Nov 10 '18 at 15:58
1
1
You are right, this really do the trick. it wasn't working with CLASS_NAME or CSS_SELECTOR but XPATH looks like it's looking for exact class.
– Aleksandar Đorđević
Nov 10 '18 at 16:23
You are right, this really do the trick. it wasn't working with CLASS_NAME or CSS_SELECTOR but XPATH looks like it's looking for exact class.
– Aleksandar Đorđević
Nov 10 '18 at 16:23
|
show 1 more comment
2 Answers
2
active
oldest
votes
@LucasTierney's answer (+1) was nearly perfect but I still feel the solution can be optimized as follows:
As the wheel is visible, instead of presence_of_element_located()
method you need to use visibility_of_element_located()
method.
The node:
<div class="wheel spinning"></div>
Can't be located through the XPath containing a single class i.e. only wheel
as in:
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
Instead you can use either of the Locator Strategies:
cssSelector
:el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.wheel.spinning")))
WebDriverWait(driver, 10).until(lambda d: 'spinning' not in el.get_attribute('class'))xpath
:el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='wheel spinning']")))
WebDriverWait(driver, 10).until(lambda d: 'spinning' not in el.get_attribute('class'))
add a comment |
You can wait for the class value to change. For example:
from selenium.webdriver.support.ui import WebDriverWait
# Wait longer than 10 seconds since you're getting occasional timeout
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
wait = WebDriverWait(driver, 10)
wait.until(lambda d: 'spinning' not in el.get_attribute('class'))
The until
method passes the driver to the method given, so you can make your own expected condition pretty easily. The above uses an anonymous lambda function but you could also use a closure or a anything callable that takes in an argument (the ExpectedConditions library is just a set of callable classes). Here is the same with a closure:
from selenium.webdriver.support.ui import WebDriverWait
# Wait longer than 10 seconds since you're getting occasional timeout
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
def wait_not_spinning(driver):
return 'spinning' not in el.get_attribute('class')
wait = WebDriverWait(driver, 10)
wait.until(wait_not_spinning)
Can you explain, what is meaning behind lambda d: ? Thanks
– Aleksandar Đorđević
Nov 11 '18 at 13:45
Lambda is just an anonymous function. Theuntil
automatically calls the passed in driver to the method given so that's what thed
argument for the lambda is. I'll update it to show the same with a closure
– Lucas Tierney
Nov 11 '18 at 15:43
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%2f53240157%2fpython-selenium-find-element-with-class-and-wait-for-class-change%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
@LucasTierney's answer (+1) was nearly perfect but I still feel the solution can be optimized as follows:
As the wheel is visible, instead of presence_of_element_located()
method you need to use visibility_of_element_located()
method.
The node:
<div class="wheel spinning"></div>
Can't be located through the XPath containing a single class i.e. only wheel
as in:
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
Instead you can use either of the Locator Strategies:
cssSelector
:el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.wheel.spinning")))
WebDriverWait(driver, 10).until(lambda d: 'spinning' not in el.get_attribute('class'))xpath
:el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='wheel spinning']")))
WebDriverWait(driver, 10).until(lambda d: 'spinning' not in el.get_attribute('class'))
add a comment |
@LucasTierney's answer (+1) was nearly perfect but I still feel the solution can be optimized as follows:
As the wheel is visible, instead of presence_of_element_located()
method you need to use visibility_of_element_located()
method.
The node:
<div class="wheel spinning"></div>
Can't be located through the XPath containing a single class i.e. only wheel
as in:
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
Instead you can use either of the Locator Strategies:
cssSelector
:el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.wheel.spinning")))
WebDriverWait(driver, 10).until(lambda d: 'spinning' not in el.get_attribute('class'))xpath
:el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='wheel spinning']")))
WebDriverWait(driver, 10).until(lambda d: 'spinning' not in el.get_attribute('class'))
add a comment |
@LucasTierney's answer (+1) was nearly perfect but I still feel the solution can be optimized as follows:
As the wheel is visible, instead of presence_of_element_located()
method you need to use visibility_of_element_located()
method.
The node:
<div class="wheel spinning"></div>
Can't be located through the XPath containing a single class i.e. only wheel
as in:
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
Instead you can use either of the Locator Strategies:
cssSelector
:el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.wheel.spinning")))
WebDriverWait(driver, 10).until(lambda d: 'spinning' not in el.get_attribute('class'))xpath
:el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='wheel spinning']")))
WebDriverWait(driver, 10).until(lambda d: 'spinning' not in el.get_attribute('class'))
@LucasTierney's answer (+1) was nearly perfect but I still feel the solution can be optimized as follows:
As the wheel is visible, instead of presence_of_element_located()
method you need to use visibility_of_element_located()
method.
The node:
<div class="wheel spinning"></div>
Can't be located through the XPath containing a single class i.e. only wheel
as in:
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
Instead you can use either of the Locator Strategies:
cssSelector
:el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.wheel.spinning")))
WebDriverWait(driver, 10).until(lambda d: 'spinning' not in el.get_attribute('class'))xpath
:el = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='wheel spinning']")))
WebDriverWait(driver, 10).until(lambda d: 'spinning' not in el.get_attribute('class'))
edited Nov 12 '18 at 12:24
answered Nov 12 '18 at 12:19
DebanjanBDebanjanB
39.1k73576
39.1k73576
add a comment |
add a comment |
You can wait for the class value to change. For example:
from selenium.webdriver.support.ui import WebDriverWait
# Wait longer than 10 seconds since you're getting occasional timeout
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
wait = WebDriverWait(driver, 10)
wait.until(lambda d: 'spinning' not in el.get_attribute('class'))
The until
method passes the driver to the method given, so you can make your own expected condition pretty easily. The above uses an anonymous lambda function but you could also use a closure or a anything callable that takes in an argument (the ExpectedConditions library is just a set of callable classes). Here is the same with a closure:
from selenium.webdriver.support.ui import WebDriverWait
# Wait longer than 10 seconds since you're getting occasional timeout
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
def wait_not_spinning(driver):
return 'spinning' not in el.get_attribute('class')
wait = WebDriverWait(driver, 10)
wait.until(wait_not_spinning)
Can you explain, what is meaning behind lambda d: ? Thanks
– Aleksandar Đorđević
Nov 11 '18 at 13:45
Lambda is just an anonymous function. Theuntil
automatically calls the passed in driver to the method given so that's what thed
argument for the lambda is. I'll update it to show the same with a closure
– Lucas Tierney
Nov 11 '18 at 15:43
add a comment |
You can wait for the class value to change. For example:
from selenium.webdriver.support.ui import WebDriverWait
# Wait longer than 10 seconds since you're getting occasional timeout
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
wait = WebDriverWait(driver, 10)
wait.until(lambda d: 'spinning' not in el.get_attribute('class'))
The until
method passes the driver to the method given, so you can make your own expected condition pretty easily. The above uses an anonymous lambda function but you could also use a closure or a anything callable that takes in an argument (the ExpectedConditions library is just a set of callable classes). Here is the same with a closure:
from selenium.webdriver.support.ui import WebDriverWait
# Wait longer than 10 seconds since you're getting occasional timeout
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
def wait_not_spinning(driver):
return 'spinning' not in el.get_attribute('class')
wait = WebDriverWait(driver, 10)
wait.until(wait_not_spinning)
Can you explain, what is meaning behind lambda d: ? Thanks
– Aleksandar Đorđević
Nov 11 '18 at 13:45
Lambda is just an anonymous function. Theuntil
automatically calls the passed in driver to the method given so that's what thed
argument for the lambda is. I'll update it to show the same with a closure
– Lucas Tierney
Nov 11 '18 at 15:43
add a comment |
You can wait for the class value to change. For example:
from selenium.webdriver.support.ui import WebDriverWait
# Wait longer than 10 seconds since you're getting occasional timeout
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
wait = WebDriverWait(driver, 10)
wait.until(lambda d: 'spinning' not in el.get_attribute('class'))
The until
method passes the driver to the method given, so you can make your own expected condition pretty easily. The above uses an anonymous lambda function but you could also use a closure or a anything callable that takes in an argument (the ExpectedConditions library is just a set of callable classes). Here is the same with a closure:
from selenium.webdriver.support.ui import WebDriverWait
# Wait longer than 10 seconds since you're getting occasional timeout
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
def wait_not_spinning(driver):
return 'spinning' not in el.get_attribute('class')
wait = WebDriverWait(driver, 10)
wait.until(wait_not_spinning)
You can wait for the class value to change. For example:
from selenium.webdriver.support.ui import WebDriverWait
# Wait longer than 10 seconds since you're getting occasional timeout
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
wait = WebDriverWait(driver, 10)
wait.until(lambda d: 'spinning' not in el.get_attribute('class'))
The until
method passes the driver to the method given, so you can make your own expected condition pretty easily. The above uses an anonymous lambda function but you could also use a closure or a anything callable that takes in an argument (the ExpectedConditions library is just a set of callable classes). Here is the same with a closure:
from selenium.webdriver.support.ui import WebDriverWait
# Wait longer than 10 seconds since you're getting occasional timeout
el = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']")))
def wait_not_spinning(driver):
return 'spinning' not in el.get_attribute('class')
wait = WebDriverWait(driver, 10)
wait.until(wait_not_spinning)
edited Nov 11 '18 at 15:46
answered Nov 10 '18 at 19:26
Lucas TierneyLucas Tierney
1,63288
1,63288
Can you explain, what is meaning behind lambda d: ? Thanks
– Aleksandar Đorđević
Nov 11 '18 at 13:45
Lambda is just an anonymous function. Theuntil
automatically calls the passed in driver to the method given so that's what thed
argument for the lambda is. I'll update it to show the same with a closure
– Lucas Tierney
Nov 11 '18 at 15:43
add a comment |
Can you explain, what is meaning behind lambda d: ? Thanks
– Aleksandar Đorđević
Nov 11 '18 at 13:45
Lambda is just an anonymous function. Theuntil
automatically calls the passed in driver to the method given so that's what thed
argument for the lambda is. I'll update it to show the same with a closure
– Lucas Tierney
Nov 11 '18 at 15:43
Can you explain, what is meaning behind lambda d: ? Thanks
– Aleksandar Đorđević
Nov 11 '18 at 13:45
Can you explain, what is meaning behind lambda d: ? Thanks
– Aleksandar Đorđević
Nov 11 '18 at 13:45
Lambda is just an anonymous function. The
until
automatically calls the passed in driver to the method given so that's what the d
argument for the lambda is. I'll update it to show the same with a closure– Lucas Tierney
Nov 11 '18 at 15:43
Lambda is just an anonymous function. The
until
automatically calls the passed in driver to the method given so that's what the d
argument for the lambda is. I'll update it to show the same with a closure– Lucas Tierney
Nov 11 '18 at 15:43
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53240157%2fpython-selenium-find-element-with-class-and-wait-for-class-change%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
1
I think you might be able to use a variant of what's here: stackoverflow.com/a/26567563/142780, specifically where they wait for a particular item (in that case found via an id rather than a class): WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
– Neil
Nov 10 '18 at 15:03
I tried this, but problem is ID always change on page, every time page refresh id of element get different, while class stays the same. I am looking also at css_selector options and xpath but none of them working for my tests.
– Aleksandar Đorđević
Nov 10 '18 at 15:07
1
But isn't there a By.CLASS_NAME? I think I'd best leave it to you, but I'm sure it's possible as did this kind of thing myself (just was a long time ago and don't have code to hand to give you the exact right syntax). NB: if you're using XPATH there are lots of simple slip ups it's easy to make, so it can be handy to test your XPATH in the Console first
– Neil
Nov 10 '18 at 15:16
1
I've just seen your XPATH edit. Does WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, "//*[@class='wheel']))) not work? Or is it matching when both classes are present? (ie wheel and spinning). I'm thinking you can skip monitoring the 'wheel spinning' version and simply wait until the appearance of a class with just 'wheel'.
– Neil
Nov 10 '18 at 15:58
1
You are right, this really do the trick. it wasn't working with CLASS_NAME or CSS_SELECTOR but XPATH looks like it's looking for exact class.
– Aleksandar Đorđević
Nov 10 '18 at 16:23