I would like to get the price value from the pseudo element you see on the picture below. If I hover over the mouse only then I can see some value like the price it costs( that is what I need). I found the "move_to_element" so now I can hover over the mouse with the program but I still cant get the price out of the element.
The problem is that even if I hover over my mouse I cant see the element opening in the inspector tab.
Thank you!
This is the code I would like to get out the :before element from:
<div onclick="Game.UpgradesById[503].click(event);" class="crate upgrade enabled" onmouseout="Game.setOnCrate(0);Game.tooltip.shouldHide=1;" onmouseover="if (!Game.mouseDown) {Game.setOnCrate(this);Game.tooltip.dynamic=1;Game.tooltip.draw(this,function(){return function(){return Game.crateTooltip(Game.UpgradesById[503],'store');}();},'store');Game.tooltip.wobble();}" id="upgrade0" style="background-position:-1056px -1296px;"></div>
After this there is an en element ::before and end of the div
For your query on how to extract only the value:
Split the output x with the delimiter of next line: \n and then get it's first index. That would give you the value. Below are the lines.
spl = int(x.split("\n")[0])
print(f'value: {spl}')
print(type(spl))
Output:
15
???
[owned : 0]
value: 15 # this line is fetched using the above code in this answer. It fetches the value from the `x`
<class 'int'>
When I visited the website manually, I get somewhat like what you showed, but when I run through automation, it does not show any Upgrades. Hence, I had to stick to checking the identities (like Grandma, Cursor, etc); but they didn't show in automated version. Only the users ??? are seen. Snapshot
Eventually, I tried it with ??? only. I tried a different approach, instead of using ::before or ::after, I tried to see if any tooltip exists somewhere, and it does exist. The only caveat is that I was able to fetch all the text from the hover (from which again we have to extract what is required through code). On this context, here is the code:
driver.get("https://orteil.dashnet.org/cookieclicker/")
time.sleep(10)
ActionChains(driver).move_to_element(driver.find_element(By.ID, 'product0')).perform()
time.sleep(1)
x = driver.find_element(By.ID, 'tooltip').text
print(x)
Here is the output:
15
???
[owned : 0]
Process finished with exit code 0
Please change the element to the element that you want and you should get the result.
Related
as you can see in the first picture, my objective is to click and open every "Ver detalles" and to get all the text within it (which is shown in the third picture.
Here is the HTML for this first screen:
And here is the screen that opens once you click "ver detalles"
And its HTML
So far this I have made up some lines of code but I know it is useless because I am looking by XPATH and not by Class (and this will only return data for one), but whenever I look by class and try to iterate it doesn't find the class.
Please let me know if I made myself clear. Thanks beforehand
EDIT:
Thanks #cruisepandey it helped me open "ver detalles". Now I'm stucked trying to get the text out of it and click de X to close it and move on to the next "ver detalles".
This is the code I have so far, i have tried looking up by class, tag, etc but can't seem to find a way :(.
def order_data():
list_of_ver_detalles = driver.find_elements(By.XPATH, "//span[contains(text(),'Ver detalle')]/..")
sleep(3)
for ver in list_of_ver_detalles:
ver.click()
print(driver.find_element_by_class_name("jss672").text)
sleep(2)
driver.find_element(By.XPATH, "/html[1]/body[1]/div[6]/div[3]/div[1]/div[1]/div[1]/img[1]").click
Here is the text I am trying to print
And here is the X I am trying to click
I want to clarify few of your doubts when you say it is useless because I am looking by XPATH and not by Class - no it is not. for finding more than one element with any locator (assuming that locator in DOM can represent multiple entity) all you have to do is to use find_elements instead of find_element.
store all of Ver detalle like this :
list_of_ver_links = driver.find_elements(By.XPATH, "//span[contains(text(),'Ver detalle')]/..")
for ver in list_of_ver_links:
ver.click()
#Now write the code to fetch order details here
Pls am trying to use the wait condition to wait for the value of an element to change before clicking it
this is the previous code;
driver.find_elements_by_xpath('//div[#data-markettypecategory="00000000-0000-0000-da7a-000000580001" and #data-pd="12.01"]')[0].click()
the code above only works when data-pd=12.01, i want to make it wait for data-pd is price (e.g $12.01), it could be any value(e.g 100.00,2.00,50.00 e.t.c), i just want it to be able to wait until the value turns to 12.01 until it clicks
first, copy FULL Xpath. NOT normal Xpath.
Full XPath looks like this:
xpath = '/html/body/div[3]/div[2]/div/div[1]/div[1]/h1/a'
You can then get the element's text value and check if it changed.
string = driver.find_elements_by_xpath(xpath).text
print(string)
if string == "12.01":
click()
My for loop goes to the latest part, and because the webpage needs the upper for loops to open so that the other for loops can operate, it doesn't work.
for x in browser.find_elements_by_class_name('node'):
x.click()
for y in browser.find_elements_by_class_name('dTreeNode'):
y.click()
if len(browser.find_elements_by_class_name('node')) > 0:
browser.find_element_by_class_name('node').click()
browser.find_element_by_xpath('/html/body/center[2]/form/table[1]/tbody/tr/td[3]/table/tbody/tr[5]/td[1]/a[1]/img').click()
browser.find_element_by_xpath('/html/body/center[2]/form/table[2]/tbody/tr/td[4]/input').click()
browser.find_element_by_xpath('/html/body/center/form/table[2]/tbody/tr/td[5]/a').click()
browser.execute_script("window.history.go(-1)")
This is the image:
In essence when the page goes back, it can't locate the last loop class and shows up an error because the element can only be found once I click on the previous element. But how do I do that inside the for loop?
Image of the HTML Text:
It seems that either the for y in browser.find_elements_by_class_name('dTreeNode'): returns an empty element or click is performed on a wrong element i.e. element is not programmed to receive the click.
Either case -
First thing - Please check if your first loop executing the code or not.
for y in browser.find_elements_by_class_name('dTreeNode'):
y.click()
print('Clicked the element') #just for debugging
Then see if the element is returned or not.
Secondly - I see you are using absolute xPath. Can you convert it to relative xPath. Refer to XPath Tutorials
If you need help on xPath, update your question and HTML snippet of the expanded tree.
I am trying to validate that a value changes to the correct text and if it does not to refresh the page and check again for up to set time.
I have tried while-loops, if statements and nested variations of both with no success. I am not even sure how to format it as this point.
element = driver.find_element_by_xpath('xpath')
While True:
if element contains textA
break
else if element contains textB
driver.refresh()
else
error
Something along those lines. Ignore any syntax errors, I am just trying to get the idea across
I have also tried using EC and By with no luck
Edit: Adding some details
So what I have is a table. I am inserting a new row with no problems. Then I need to check that one of the column values of the new row gets updated from 'new' to 'old' which usually takes about anywhere from 30secs to 2mins. This is all viewable from a web ui. I need to refresh the page in order to see the value change. I wish I had some more detailed code or error to post along with it but honestly I am just beginning to learn Selenium
Can you please try the following :
while True:
try:
driver.find_element_by_xpath('xpath'):
except NoSuchElementException:
driver.refresh
else:
print("Text found")
break
Note: I suggest to create text-based XPath to avoid an extra line of code to get and compare text.
I am using Python Selenium trying to get some data from a website and need to change the day of a date.
I tried the following: Get the table with all dates and iterate through all tds. If the right day appears click. Unfortunately that does not work. It prints the correct numbers but it does not click on the one it should or any.
day_table = depar_date.find_element_by_xpath("/html/body/div[8]/section/div/div/div[2]/div/table/tbody")
day_table.click()
for row in test.find_elements_by_css_selector('tr'):
for cell in row.find_elements_by_tag_name('td'):
print(cell.text)
if cell.text == "15":
cell.click()
I get the following error message:
StaleElementReferenceException: stale element reference: element is
not attached to the page document
I also see that for the selected day aria-pressed = "true", is there a way to set this "true" for the correct day?
many thanks for any help.
I guess you need to perform the action on the button itself, not the "tr" element because the event listener is on the button. Could you try to do the following and let me know what happens:
depar_date.find_element_by_xpath("//td/button[text()="15"]").click()