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.
Related
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
Python newbie here. All of 1 month old. Most of the elements in page i am trying to scrape, i am able to handle them and interact with them. there are two elements that have dynamic labels which i am not able to handle. The source page looks as below
<span class="a-button-inner">
<input class="a-button-input" type="submit" aria-labelledby="Ae8MCi-55">
<span id="Ae8MCi-55" class="a-button-text" aria-hidden="true">Add Your Key</span>
</span>
Upon each refresh the label is new so i cannot get a fixed XPATH of that. There are multiple items with span class "a-button-inner" as well as input class "a-button-input" and there are other submit buttons as well in the rest of the page. The only thing unique is the Span text "Add Your Key".
Appreciate all help to get the submit button element and to click/submit it.
from selenium import webdriver
.
.
.
.
# objAddKey = driver.find_element_by_xpath('//*[#id="Ae8MCi-55"]/span/input') does not work as second round its a different XPath
objAddKey = driver.find_element_by_link_text('Add Your Key') # hoping this will get a sibling and then to get the parent and then look for all the children. I don't even know if its possible in python.
objAddKey.click()'
Tried to search for searching by span text, and came across some other stuff,
https://www.tutorialspoint.com/how-to-get-text-found-between-span-selenium
WebElement l = driver.findElement(By.xpath("//p/span"));
String s = l.getText();
but this does not help either.
https://selenium-python.readthedocs.io/locating-elements.html
went through this, but not much help either.
Appreciate your help and pointers.
Thank you.
If Add Your Key is unique then you can use the below xpath :
//span[contains(text(), 'Add Your Key')]//preceding-sibling::input[#class='a-button-input']
or even without class :
//span[contains(text(), 'Add Your Key')]//preceding-sibling::input
and use it like this :
objAddKey = driver.find_element_by_xpath("//span[contains(text(), 'Add Your Key')]//preceding-sibling::input")
objAddKey.click()
I would suggest you to have explicit waits for more reliability.
Selenium - Python - Explicit waits
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.
I have a website that I'm trying to automate but I can't find a particular link with selenium to click on. It looks like a link on the website, but when I use the chrome "inspect" function, it looks like it might be a button (???). I've tried copying the Xpath, but that doesn't work.
Here is the HTML behind the link
<button ng-bind-html="::ListingCtrl.copy.planListing.noPreference" track="No Preference" ng-click="::ListingCtrl.enterNoPreference()" class="link ally-focus-within">No Preference</button>
The Xpath for it is
//*[#id="mainContent"]/div/div/div/div[2]/ul/li[1]/h2/button
The text of the link is "No Preference", so also tried the following
elem_NoPreference = browser.find_element_by_xpath('//#track=\'No Preference\'')
But I'm not sure if my quote escape characters are correct.
I also unsuccessfully tried the following
elem_NoPreference = browser.find_element_by_link_text('No Preference') <br>
elem_NoPreference = browser.find_element_by_class_name('link ally-focus-within') <br>
elem_NoPreference = browser .find_element_by_css_selector("button[class='link ally-focus-within']
I should mention that the following are unique in the HTML. So, if there is a way to find these using the Xpath, it would be helpful
ListingCtrl.copy.planListing.noPreference
ListingCtrl.enterNoPreference()
track = "No Preference"
I'm at my wits end here. Any help would be appreciated.
Thank you!
If you don't plan to do any I18N / L10N testing you can stick to No Preference text, in this case the selector would be:
//button[text()='No Preference']
other option is basically the same, but instead of text it looks for track HTML attribute:
More information:
XPath Tutorial
XPath Operators & Functions
Xpath cheatsheet
Try with this xpath:
elem_NoPreference = browser.find_element_by_xpath("//button[#track='No Preference']")
or, if you want to select the element by the containing text:
elem_NoPreference = browser.find_element_by_xpath("//button[contains(.,'No Preference')]")
I'm trying to click the following link using selenium.
<div id="RECORD_2" class="search-results-item">
<a hasautosubmit="true" oncontextmenu="javascript:return IsAllowedRightClick(this);" class="smallV110" href="#;cacheurlFromRightClick=no"></a>
</div>
Which record to click is not known before the code is executed. Record_2 has multiple children, and the one included is the one I want to click. The link is edited for the sake of privacy. I tried to do something like that where name is the record variable, however it doesn't work.
driver.find_element_by_css_selector("css=div#"RECORD_%s" % (name).smallV110")
I'm a complete newbie to selenium so I couldn't figure out a way to sort this out. I would appreciate any help. Thanks!
Note that this is not Selenium IDE and you don't need the css= at the beginning of a selector.
There are multiple ways to locate the link element, e.g.:
driver.find_element_by_css_selector(".search-results-item a.smallV110")
driver.find_element_by_css_selector("[id^=RECORD] a.smallV110") # id starts with "RECORD"
If you know the id value beforehand:
id_i_know = 2
driver.find_element_by_css_selector("[id=RECORD_%d] a.smallV110" % id_i_know)
You don't have to have that smallV110 class attribute check - I've added it to increase chances of not matching other a elements inside the div (not sure what they are, you have not posted the entire HTML).