How to select a checkbox with Selenium WebDriver in Python - python

I'm a real noob and I started to learn Python a few weeks ago.
I need to create an automation for eCommerce, I need to log in, select a page, **select a checkbox ** and click the button "submit". It seems very simple I know but on this website, the normal procedure doesn't work:
This is the HTML code:
<div class="loader-container"\>
<table class="table-grey table__orders table"\>
<thead\>
<th class="check-all table__orders-checker"\>
*\*\<div class="check-all__box"\>
<input type="checkbox" value="checkAll"\>\*\*
</div\>
This is what I coded to click the "check all" checkbox:
driver.find_element(
By.xpath, "//div[contains(#class, 'loader-container')]/table/thead/tr/th/div/input[#type='checkbox']").click()
I've tried also:
WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
By.XPATH, "//div[contains(#class, 'loader-container')]/table/thead/tr/th/div/input[#type='checkbox']")).click()
Both without success, nothing happens when it should click this checkbox, I've also other try looking on this forum or YouTube but I'm missing something because I can't fix it.
Thank you in advance for your patience!

Related

Can't Click Radio Button in Selenium Using Python

Here is the HTML Code:
<div class="ui-helper-hidden-accessible">
<input type="radio" name="group2" value="yes">
</div>
Full script
Here are the code which i've tried to click the radio button:
driver.find_element(By.CSS_SELECTOR, 'input[name= "group2"][value="yes"]').click()
AND
rdbutton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[name= "group2"][value="yes"]'))).click()
AND
rdbutton = driver.find_elements(By.XPATH, "//input[#name='group2']")
for rdbuttons in rdbutton:
if rdbuttons.is_selected():
pass
else:
rdbuttons.click()
It seems that you are using wrong locator. From the HTML you have given i think you are trying to click on radio button that have label Ya if this is the case then try with below xpath:
"//label[contains(text(),'Ya')]"
OR
"//*[normalize-space()='Ya']"
If this does not solve your problem, so please explain and add HTML of proper element either by screenshot or in textual format

Selenium click on button trouble (Python web scraping)

I am trying to web scraping a popular movie database on the Internet. After a few thousands of requests, my spider is detected and I have to manually click on a simple button to continue my data collection.
I have tried to automatize this process by clicking automatically in that button. For that purpose, I am using Selenium library. I have located the xpath of the button, but, for some reason, .click() method doesn't work.
Here is my code:
def click_button():
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")
DRIVER_PATH = 'geckodriver'
driver = webdriver.Firefox(options=options, executable_path=DRIVER_PATH)
driver.get('https://www.filmaffinity.com/es/main.html')
element = driver.find_element(By.XPATH,"//input[#value='Send']")
element.click()
driver.quit()
Also, I have tried other common alternatives, such as waiting until the element gets clickable or visible, but this didn't work. I have also tried to execute the click as a javascript script.
This is the window I see when my spider is detected:
Too many request window (picture)
As you see, there is a reCaptcha-protection icon on the lower right corner, but I don't have to solve any captcha puzzle to confirm I am not a robot, I have just to click on the send button you can see on the picture.
The html which contains the button is the following one:
<div class="content">
<h1>Too many requests</h1>
<div class="image">
<img height="400" src="/images/too-many-request.jpg">
</div>
<div class="text">
Are you sure you do not dream of electric sheep?
</div>
<form name="toomanyrequest">
<div class="alert">
please enter the Captcha to continue.
</div>
<div>
<input type="submit" value="Send">
</div>
</form>
</div>
Which do you think is the trouble with my code or approach? I have to find a way to click on that button to continue my scraping, but I don't know what I am doing wrong (I have little experience at this field).
Maybe the xpath is not correct? Maybe the captcha protection is blocking the click action? I don't get any exception when I execute my code; but nothing happens and the "Too many request" window does not disappear.
Thank you very much.
Try this:
driver.execute_script("arguments[0].click()", element)
or:
driver.execute_script("arguments[0].click();", element)

Python Selenium - Error: Element not Interactable. Interactive with website pop up window

I'm running a web scraper on my company's website so I can create elements every month. In testing, I have everything working until I get to interact with a pop up menu.
Code that interacts with pop up:
driver.get("www.website.com")
driver.find_element(By.ID, "ButtonCreatePeriod").click()
time.sleep(1)
driver.find_element(By.ID, "NoExpiration").click()
time.sleep(1)
driver.find_element_by_css_selector('[class="k-widget k-dropdown shorterDropDown"]').click()
time.sleep(3)
ddelement2 = driver.find_element_by_xpath("//*[text()='December 2021']")
action2 = ActionChains(driver)
action2.click(on_element=ddelement2).perform()
On action2.click(on_element=ddelement2).perform() I am getting the error:
"Message: element not interactable: [object HTMLLIElement] has no size and location"
I'm guessing this has to do with interacting with the popup.
Within the pop up that is opened with I click "ButtonCreatePeriod" I am opening the drop down menu defined as class="k-widget k-dropdown shorterDropDown".Within this drop down, I need to select the option with the text "December 2021". I cannot use IDs or numerical values here since this varies across other UIs in the website. The options shown on the dropdown are scrollable which could be an issue too.
I used this same code on another UI on the website with no issues but that one did not present a pop up, although it was scrollable.
Any ideas as to how I could have it select "December 2021" here?
Error generated per #cruisepandey suggestion:
Message: element click intercepted: Element <span title="" class="k widget
k-dropdown shorterDropDown k-state-disabled ic-dropdown-readonly"
unselectable="on" role="listbox" aria-haspopup="listbox" aria-
expanded="false" tabindex="0" aria-
owns="MfrCoreTermEffectivePeriodKey_listbox" aria-live="polite" aria-
disabled="false" aria-readonly="true" aria-busy="false" aria-
activedescendant="l581310d-64f8-4bbc-9354-71b6f041d0e3" style="">...
</span> is not clickable at point (434, 383). Other element would
receive the click: <p>...</p>
(Session info: chrome=93.0.4577.82)`
Solution
I basically had to dig deeper into the HTML code and use xpath to further define the lists within the drop down.
driver.find_element_by_css_selector('[class="k-widget k-dropdown shorterDropDown"]').click()
time.sleep(1)
driver.find_element_by_xpath("//div[#id='MfrCoreTermExpirationPeriodKey-list']//li[text()='December 2021']").click()
Possible reasons could be
Screen is not maximized, if so please maximize it when you launch the URL.
driver.maximize_window()
ActionChain may help.
time.sleep(5)
ActionChains(driver).move_to_element(driver.find_element(By.CSS_SELECTOR, ".k-widget.k-dropdown.shorterDropDown")).click().perform()
JS intervention with scrollInto view may help.
time.sleep(5)
ele = driver.find_element(By.CSS_SELECTOR, ".k-widget.k-dropdown.shorterDropDown")
driver.execute_script("arguments[0].scrollIntoView(true);", ele)
Imports :
from selenium.webdriver.common.action_chains import ActionChains

Why selenium(python) can't find button?

When I enter my account and make request to the invite link and turns out that invite link expired, selenium correctly determines the button "Continue to Discord":
button = driver.find_element_by_css_selector("[type='button']")
But when invite is still valid, selenium can't determine button "Accept Invite". Codes of these both buttons:
<button type="button" class="marginTop40-i-78cZ button-3k0cO7 button-38aScr lookFilled-1Gx00P colorBrand-3pXr91 sizeLarge-1vSeWK fullWidth-1orjjo grow-q77ONN"><div class="contents-18-Yxp">Accept Invite</div></button>
<button type="button" class="marginTop40-i-78cZ button-3k0cO7 button-38aScr lookFilled-1Gx00P colorBrand-3pXr91 sizeLarge-1vSeWK fullWidth-1orjjo grow-q77ONN"><div class="contents-18-Yxp">Continue to Discord</div></button>
Why can't selenium find first button if their codes are actually the same? I tried almost all methods. By xpath, by css selector and by class. Nothing helped. Seems like changing browser doesn't help too. Any ideas?

Select option with selenium

<paper-radio-button name="NOT_MADE_FOR_KIDS" class="style-scope ytcp-audience-picker iron-selected" role="radio" tabindex="0" toggles="" aria-checked="true" aria-disabled="false" aria-selected="true" checked="" active="">
<div id="radioContainer" class="style-scope paper-radio-button">
<div id="offRadio" class="style-scope paper-radio-button"></div>
<div id="onRadio" class="style-scope paper-radio-button"></div>
<paper-ripple id="ink" center="" class="circle style-scope paper-radio-button" checked="">
I am trying to select the 'No, it's not made for kids' option when uploading a video to youtube,
I do
kids_box = driver.find_element_by_name('NOT_MADE_FOR_KIDS')
kids_box.click()
Also tried with class name and id, I tried clicking the id='onRadio' too. Any help on this is appreciated!
You are just clicking to the button as a whole and I think it is wrong.
Instead, you should be clicking to the desired option on the radio button. In order to do this, please try the code snippet below.
kids_box = driver.find_element_by_id('onRadio')
kids_box.click()
If the above solution does not work, what you should be doing is, right click to the option circles in the radio button, then click inspect in Google Chrome, then right click to the button again and then click inspect again.
After completing this progress, you will see the corresponding line of the radio button. Right click to it, select copy, then select copy xpath and use the function below:
kids_box = driver.get_element_by_xpath("//div[#id='a']/div/a[#class='click']")
kids_box.click()
You should paste the xpath to inside the parantheses.

Categories

Resources