Python selenium cannot click or change value in radio button - python-3.x

Trying to automate amazon affiliate report generation. cannot change the value of a checkbox
need to mark this checkbox
this is HTML of that checkbok
radio = driver.find_element_by_xpath(
"//input[#value='custom' and #name='ac-daterange-radio-report-download-timeInterval']")
driver.execute_script("arguments[0].click();", radio)
# driver.execute_script(
# "arguments[0].setAttribute('checked', 'checked';", radio)
If more clarity needed can ask here.
thanks in advance

You are not sending click event to java script executor, instead of this you directly click by .click() method. If you want to click one of the checkbox you use one of the following commands, change the text contains: Today, Yesterday and other. See this:
driver.find_element_by_xpath("//span[contains(text(), 'Today')]/ancestor::label/input").click()
driver.find_element_by_xpath("//span[contains(text(), 'Yesterday')]/ancestor::label/input").click()
driver.find_element_by_xpath("//span[contains(text(), 'Custom Date Range')]/ancestor::label/input").click()

Related

Python- selenium webdriver is not able to find these buttons on modal window

Not able to click x or continue to flights
Here is the url: https://www.kayak.com/flights/ORD-JFK/2020-09-13/2020-09-20?sort=bestflight_a
using css = '.Button-No-Standard-Style.close-button'
I was using the following to find and click it:
driver.find_element_by_css_selector('.Button-No-Standard-Style.close-button').click()
What I found out was the button had dynamic ID. I had to click the button based on partial ID comparison.
I used:
cl_bt = driver.find_element_by_xpath("//*[contains(#id, 'covid-loading-dialog-close')]")
where 'covid-loading-dialog-close' was the part of the ID that was not changing.

Selenium Scroll down on Newly Opened Body

I am working with Selenium python, and would like to scroll down on a page after clicking an element. Clicking the element gives a pop-up menu, and then when I try some scrolling methods they scroll on the original page.
I have selected the body of the new element that has appeared and tried this:
from selenium import webdriver
from selenium import Keys
driver = webdriver.get(url)
button = driver.find_element_by_xpath(pathto)
button.click()
body = driver.find_element_by_css('body')
body.send_keys(Keys.PAGE_DOWN)
This does not actually do anything as is. However, if I first manually click on the new popup element before doing body.send_keys(Keys.PAGE_DOWN), it does scroll.
One way I have resolved this is using ActionChains to right click on the body before scrolling down. Directly using .click() on the body does not work as it clicks a link in the body.
Are there any better ways to do this? In particular, I am somewhat afraid that the right click menu that opens up when I use my current method may mess with the rest of the scraping. Is there at least a method to guarantee that it does not interfere? Another solution could be somehow "selecting" or clicking on the body element within selenium, so are there any suggestions for this?
You could try using END key instead of DOWN key. It's worth a shot:
from selenium.webdriver.common.keys import Keys
driver = webdriver.get(url)
button = driver.find_element_by_xpath(pathto)
button.click()
driver.find_element_by_tag_name('body').send_keys(Keys.END)
You also may need to click the pop-up in order to focus it first:
from selenium.webdriver.common.keys import Keys
driver = webdriver.get(url)
button = driver.find_element_by_xpath(pathto)
button.click()
# click popup menu to get it into focus
popup_menu = driver.find_element_by_xpath(pathToPopupMenu)
popup_menu.click()
driver.find_element_by_tag_name('body').send_keys(Keys.END)

Robot- Selenium : How we can handle a element (text box) whose ID is changing dynamically

I am facing one simple problem-- there is one Add button in the web page, After clicking that a small pop up window (Not in another browser tab , without title) opened having 3 text box. These id/xpath is getting changed whenever will click on Add button. I need to to enter few data and then save.
Could you please help me how we can handle this dynamic xpath/id ---text box in robot selenium framework. Is there any way we can input on text box based on label. Please share any pointer or sample code.
Thank you very much for the help.

not able to click on a button for scraping using selenium and python

THIS IS THE BUTTON CODE WHICH I WANTED TO CLICK
I used this code.
login_btn1 = driver.find_elements_by_tag_name('button')
login_btn1.submit()
but they are showing the error:
AttributeError: 'list' object has no attribute 'submit'
can someone help me with this please.Where am i going wrong?
By using find_elements_by_tag_name you are looking for all 'button' elements which will return a list.
You need to replace this with find_element_by_tag_name to retrieve a single 'button' element. (this will be the first button element found)
Alternatively, in the likely situation that there is more than one button on the page, you can select by class name using find_element_by_class_name('cdp-view-all-button') or another method from the selenium docs.
When you write this :
login_btn1 = driver.find_elements_by_tag_name('button')
Now login_btn1 is a list of Web Element. you can have method which are applicable for list.
Since you want to click on button and button is a web element. you can't do login_btn1.click() .
If you want to click on a single button, first you have to find the locator then there are multiple ways to click on it such as :
button = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#data-track-app='discovery']")))
button.click()
If the provided xpath is Unique in DOM then the above code would work.

How to identify a button click in coded UI Customised code

In UIMAP.CS file in both the Recorded method and assertion method, I am not able to identify the button click.
Is there any way I can get hold of the button click event?
I am able to get hold the button in to a UITESTCONTROL variable but the options I was able to see are "exists", "enabled","name" etc...
I tried checking it with assertions too when I dragged the cross hair on to the clicked button , assertions are generated only for checking the existence,correctness of text and name but not the events occured on it..
Please help in this regard..
Please make sure that the Control/Object on which you want to use the Click operation is a button and not any Web Element or control which does not support the Click operation.
Once verified, you can do the following:
Record a New Session and while recording click on that button, stop your recording.
Open the UIMap.cs and navigate to that button control. The intellitrace would show you all the available options for that control.
Thanks
Nikhil

Resources