I am trying to automate a process of clicking some buttons. Although I have come really far and only 1 button that is not being able to work . I am using python with selenium. So I just want to click this button but I am unable to do so. Below is my code I have tried with css select and by xpath but Still I am unable to click it , I am getting error path not found.
This is the button that I want to click
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit" type="button" onclick=";return false;" data-channel-creation-token="GhaqucG9ARAKDi9teV92aWRlb3M_bz1VKAQ%3D"><span class="yt-uix-button-content">CREATE CHANNEL</span></button>
I have tried the following 2 codes but none of them work.
driver.find_element_by_xpath("//button[#class='button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']").click()
driver.find_element_by_css_selector('button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit').click()
Let's go over your attempts:
driver.find_element_by_xpath("//button[#class='button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']").click()
This one did not work because you are trying to put a CSS selector into a #class attribute value check. You meant to do something like:
//button[#class='yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']
driver.find_element_by_css_selector('button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit').click()
This one did not work since you are not specifying multiple classes in a CSS selector correctly, classes need to be separated with a dot:
button.yt-uix-button.yt-uix-button-size-default.yt-uix-button-primary.create-channel-submit
Note that a much simpler selector should do the job - you don't have to specify all classes in a CSS selector - pick a more data-oriented and unique one, in this case I think this should be reasonably reliable and readable:
driver.find_element_by_css_selector('button.create-channel-submit').click()
Assuming you want to click on the button CREATE CHANNEL you need to consider the presence of the <span> tag, within the <button> tag. You can use the following line of code:
driver.find_element_by_xpath("//button[#class='yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']/span[class='yt-uix-button-content']").click()
You can click on your element with next method:
driver.find_element_by_xpath("//span[(#class='yt-uix-button-content') and contains(text(), 'CREATE CHANNEL')]/..").click()
You can try this code
driver.find_element_by_css_selector('button.yt-uix-button.yt-uix-button-size-default.yt-uix-button-primary.create-channel-submit').click();
can you try using by_class_name. You are using xpath and css to look up the class name
driver.find_element_by_class_name('yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit')
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"))
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 am trying to select from a list of 3 buttons, but can't find a way to select them. Below is the HTML I am working with.
<input name="pollQuestion" type="radio" value="SRF">
<font face="arial,sans-serif" size="-1">ChoiceOne</font><br />
<input name="pollQuestion" type="radio" value="COM">
<font face="arial,sans-serif" size="-1">ChoiceTwo</font><br />
<input name="pollQuestion" type="radio" value="MOT">
<font face="arial,sans-serif" size="-1">ChoiceThree</font>
I can find it by using the following code:
for i in browser.find_elements_by_xpath("//*[#type='radio']"):
print i.get_attribute("value")
This outputs: SRF,COM,MOT
But I would like to select ChoiceOne. (To click it) How do I do this?
Use CSS Selector or XPath to select by value attribute directly, then click it.
browser.find_element_by_css_selector("input[type='radio'][value='SRF']").click()
# browser.find_element_by_xpath(".//input[#type='radio' and #value='SRF']").click()
Corrections (but OP should learn how to look up in documentation)
In Python binding, find_elements_by_css doesn't exist, it's called find_elements_by_css_selector. One should be able to look at the exception message and look back into documentation here and figure out why.
Notice the difference between find_element_by_css_selector and find_elements_by_css_selector? The first one finds the first matching element, the second one finds a list, so you need to use [0] to index. Here is the API documentation. The reason why I use the latter, is because I copied your code, which I shouldn't.
Selenium webdriver Radio button click
When i used xpath :
driver.find_element_by_xpath("//input[#id='id_gender2']").click()
radio button not selected
But I used css_selector :
driver.find_element_by_css_selector("input#id_gender1").click()
radio button selected
find_elements_by_css_selector worked for me,
browser.find_elements_by_css_selector("input[type='radio'][value='SRF']")[0].click()
First Radio button was not selected for me also. But after inserting Time it works for me.
driver.find_element_by_class_name("login").click()
driver.find_element_by_id("email_create").send_keys("testsel000#gmail.com")
driver.find_element_by_id("SubmitCreate").click()
time.sleep(2)
driver.find_element_by_css_selector("#id_gender2").click()
Consider that you have a radio button to select either of the two options, "Male or "Female". Then try using the following :- This is in Python (Selenium).
driver.find_element_by_xpath("//label[contains(text(),'Male')]").click()
browser.find_elements_by_xpath(".//input[#type='radio' and #value='SRF']")[0].click
This ended up being the fix. I was getting errors without the [0] there, that a list does not have a click() attribute (even though there was only 1 match). Thanks for the help user1177636!