I know the basics of python and I am trying out Selenium. This page I am on is my schoolwork website. I want to select every single element in the timeline of my homework. I tried to select the whole timeline with XPath or even just one specific homework(elem) but both have this error code:
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="jwdd3b016b_acnt"]/div[3]/ul/li[3]"}
(Session info: chrome=81.0.4044.122)
This is the code:
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\tomas\\Downloads\\chromedriver_win32\\chromedriver')
driver.get('https://glnt.edupage.org/timeline/')
#then I log in but that's not important
important_stuff = driver.find_element_by_xpath('//*[#id="jwdd3b016b_acnt"]/div[3]/ul/li[3]')
I have no idea what to do. I was under the impression that when u use XPath the code should always work. Am I wrong? (not always I mean, but when the code is correct)
Edit: sorry I forgot here is the HTML of the element:
<li class="hwItem m04 d5" data-typ="timeline" data-homeworkid="timeline:2031705" data-date="2020-04-24" data-timelineid="2031705">
That should do it:
<div class="edubarMainNoSkin">
<div id="jwdd3b016b_acnt" class="hwMainListMain">
<div class="hwListElem">
<ul class="hwList timelineMode ">
I couldn't say much without seen more of the HTML.
//*[#id="jwdd3b016b_acnt"]/div[3]/ul/li[3]
id part may not be constant.
It might be inside of an iframe, so you have to switch into it.
Due to browser rendering, deepness of the div might be changed.(div[3] would be changed)
First find the correct element using developer tools.
Then while executing using webdriver put a break point and check via developer tools.
Related
I want to click some element (date) in date picker popup using selenium python, if I define the text it can be clicked. but, when I change it with formatting it not worked and I don't know why.
I already tried this way with formatting string in python way:
chosen_date = str(day_date-7)
date_choose = driver.find_element(By.XPATH, "// div[contains(text(), '{}')]".format(chosen_date))
HTML:
<div class="shopee-react-date-picker__table-cell-wrap">
<div class="shopee-react-date-picker__table-cell selected">6
</div>
</div>
but, it never worked and always throw an error and stop the script automatically. I'm quite new in using selenium to automate website, is there any possibilities to overcome this error?
Thank you so much in advance.
as I can see is there is a space in // div section of your selector. There should be not any space:
//div[contains(text()
the rest is ok.
I have a script going using Selenium and on a new window pop up I am trying to retrieve this element:
<div class="custom-control p-2">somenumber</div>
The full path to this class is
html/body.modal-open/div#modalUserPRMLead.modal.fade.show/div.modal-dialog/div.modal-content/div.modal-body/div.row.justify-content-center.p-3/div.col-md-12/div.custom-control.p-2
I have tried variants of these to grab just the text of 'somenumber'
driver.find_element(By.CSS_SELECTOR,'row justify-content-center p-3').get_attribute('innerHTML')
driver.find_element(By.XPATH("html/body.modal-open/div.col-md-12./div.custom-control.p-2")).text
driver.find_element(By.XPATH("custom-control p-2")).text
driver.find_element(By.XPATH("div.row.justify-content-center.p-3")).text
driver.find_element(By.XPATH,custom-control p-2).text
driver.find_element(By.XPATH,div.custom-control.p-2).text
The errors I get is that it cannot locate the tag from current session. It's a new window that pops up as a result of previous script, I did driver.current_url just to make sure it was capturing the link to the new window in current session and it was.
I'm not understanding how to index down to the div class and just grab the text. Any help would be greatly appreciated. The script prior to window pop for form input was much easier because I could tag everything using By.NAME or By.ID
Your CSS selector is wrong. XPaths don't really looks like XPaths...
Try
driver.find_element(By.XPATH, '//div[#class="custom-control p-2"]').text
or
driver.find_element(By.CLASS_NAME, 'custom-control').text
In case element is dynamic you might need to apply wait
You can also check basic syntax of XPath and CSS-selector
Here is the inspect result for the button that says +5 per day
>span class="text user-links entry-method-title ng-scope ng-binding" ng-include="::'views/entry-text/'+entry_method.entry_type+'.html'">
Click For a Daily Bonus Entry"
</span>
<div class="entry-method bonus template" data-remove-popovers="" id="em6129519" ng-class="{expanded: entryState.expanded == entry_method, template: entry_method.template, 'completed-entry-method': !canEnter(entry_method) && isEntered(entry_method)}" ng-repeat="entry_method in ::entry_methods">
here is the HTML given information when I inspect the link/button, I have tried to use XPath, CSS, link text, and class name and it keeps giving me an error saying it cannot identify the element. Does anyone have a suggestion for how to identify this, it is on gleam.io for a giveaway I'm trying to automate this so i don't have to log in and press this everyday. This is my first ever web interfacing project with python.
Here is my most recent try
driver.maximize_window()
time.sleep(10)
driver.execute_script("window.scrollTo(0, 1440)")
time.sleep(10)
button2 = driver.find_element_by_class_name("text user-links entry-method-title ng-scope ng-binding")
button2.click()
Similar to a previous issue, Selenium find_element_by_class_name and find_element_by_css_selector not working, you can't have spaces in your class name when using driver.find_element_by_class_name. Instead, find the element via css_selector and replace each space with a dot.
driver.find_element_by_css_selector("span.text.user-links.entry-method-title.ng-scope.ng-binding")
That'll fix what you have above, but keep in mind there are other ways to make selenium actions more reliable (eg. WebDriverWait, etc). And there may be a cleaner selector to use than the one above.
I believe the element you want to access is contained within an "iframe", thus you must first switch to iframe before you can access it using selectors.
driver.switch_to.frame(x.find_element_by_xpath("PUT IFRAME XPATH HERE"))
I'm a newbie with Selenium trying a project out and I'm trying to click on a link on a page but Selenium says it could not find the element. Here is the inspected element:
<a data-action="renditions--schedule-item-list#showAvailableItems" class="available-items-btn">
<i class="icon-bullhorn"></i>
Available Items
</a>
Here is what I've tried:
driver.find_element_by_xpath('//a[text()="Available Items"]').click()
and
driver.find_element_by_class_name("a.available-items-btn").click()
Any idea what's wrong?
Sometimes finding by exact text match doesn't always work. Try this:
driver.find_element_by_xpath("//a[contains(text(), 'Available Items')]")
Or, try your second attempt again but remove the a.
driver.find_element_by_class_name("available-items-btn").click()
I am trying to find an element in the web page using selenium. when I "right click" on the web element and inspect, I can find the html code in the console. However when I copy the xpath of the same element from the console and try to execute it in firepath, it says "No matching nodes". Why is it so and how can I fix this ?
Here is the HTML of the element.
<input id="mobile" type="text" onchange="javascript:dispLocMob(this);
"onkeydown="javascript:dispLocMob(this);
"onkeyup="javascript:dispLocMob(this);" value="" maxlength=
"10" placeholder="Mobile Number" name="mobile">
And this is what I am doing:
receiverMobileElement = browser.find_element_by_xpath('//*[#id="mobile"]')
Please help.
Why using xpath when you can find the element by unique id?
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://example.com')
target_element = browser.find_element_by_id('mobile')
I always say screw firebug, use the Chrome console..
From the looks of it, the best way to get this particular element would be one of the following two options (given that 'id=mobile' is unique)..
By.cssSelector("#mobile");
By.xpath("//input[#id = 'mobile']");
To use the chrome console -->
Start Chrome
Right click and inspect your element.
You should see the html code and the console appear below it. (If you can not see the console, click the three vertical dots and select 'Show Console'
Now, from here you can do some awesome stuff! You can search for the elements that you are trying to identify and see if they can be found. for example, to see if the By.cssSelector("#mobile"); will work type this
document.querySelectorAll("#mobile")
for the xpath, type this
$xBy.xpath("//input[#id = 'mobile']");
Just a suggestion... dont be concerned about firebug if your code works, and seriously try out this way in chrome!!