Attempting to click a button in Python using Selenium WebDriver (Firefox) - python

I am attempting to click a button on a html page using Python and selenium web driver.
This is the source code of the page http://pastebin.com/112g1Gje.
I believe the relevant portion is at the end. I'm trying to click the button that says "Message"
Normally I would do something like:
driver.find_element_by_id("message-modal").click()
However that doesn't work.
I have tried:
driver.find_element_by_id("message_label").click()
driver.execute_script('document.getElementByName(" Message ").click();')
driver.execute_script('document.getElementById("message-senderId").click();')
driver.execute_script('document.getElementById("message- label").addEventListener("submit", function())')
...etc.
All of them don't work.
For the stars by the way I had the same issue. It was hard to click it, but I figured this part out. This worked:
driver.execute_script('document.getElementById("star_41094_4").checked = true;')
I think this page is switching up the numbers for the star. So that number may not work right now. But that's a separate issue. Does anybody know?
EDIT: I have asked a moderator to delete this thread. I had a number of things wrong here. I am creating a new one.

Try
driver.find_element_by_xpath("//*[text()='Open Message Modal']").click()
Happy Coding :)

I think you forgot to code a button that opens that message-modal. Better create that button first like.
<button class="btn" id="btn-message-modal" data-toggle="modal" data-target="#message-modal"> Open Message Modal</btn>\
Then try this:
driver.find_element_by_id("btn-message-modal").click()
PS
message-modal is the id of the modal container that is why nothing happens
On this code
driver.find_element_by_id("message-modal").click()

driver.find_element_by_classname("btn").click() works

Related

I want to download F&O BhavCopy from the below link using Selenium Python

https://www1.nseindia.com/products/content/derivatives/equities/archieve_fo.htm
Here after entering the required details, I have tried many times to click on "GetData" button, but Selenium doesn't click on the button. I have tried Below lines of code to click on button but still got no luck.
driver.find_element(By.CSS_SELECTOR,"input[src='/common/images/btn-get-data.gif'][type='image']").click()
driver.find_element(By.XPATH,"//input[#src='/common/images/btn-get-data.gif' and #type='image']").click()
driver.find_element(By.CLASS_NAME,"getdata-button").click()
Here is the element from which I am trying to click on the button,
<input type="image" class="getdata-button" src="/common/images/btn-get-data.gif" onclick="validateInput()">
I tried to create a small script for that and I see what you mean. Nothing happens after the click.
I analyzed the behavior and I see that the click happens but for some reason all the requests hang in Pending status.
So, after the click, a request https://www1.nseindia.com/ArchieveSearch?h_filetype=fobhavzip&date=08-11-2022&section=FO is sent but it doesn't return any data. Nothing wrong with your code but I think they somehow detect a "bot" and stop processing all requests from this browser session. You cannot even refresh the page.

How can I click "invisible" reCAPTCHA buttons using Selenium web automation?

I am using Python and Selenium to automate this website: https://prenotami.esteri.it
The script I made fills out a form and then clicks a button to advance to the next page. These actions are carried out using Selenium's find_element_by_xpath() function. Recently, the website added a reCAPTCHA that pops up after the button is clicked, and must be completed before advancing.
I have already written a Python script that is capable of surpassing this type of captchas by using the audio option. However, in this particular website, I am not able to find the xpath to the audio button of the reCAPTCHA. Although there is an iframe that contains the reCAPTCHA, there seems not to be anything inside it.
In the first attached image you can see how this website's reCAPTCHA looks like in HTML, compared to other website that is visible in the second image, where a #document can be seen inside the iframe.
My intention is to run this program using headless Chrome, so I can't relay in any mouse control functions offered by pyautogui for example.
I've been scratching my head around this problem for a while, so any advice is useful. Thanks!
Edit: after some research I have found that this type of reCAPTCHA that doesn't need to check a "I am not a robot" checkbox is called "invisible reCAPTCHA". The captcha only pops up if the detected activity is suspicious (for example clicking too fast). I have tried adding random waits and movements to mimic human behaviour, but the captcha still appears after some tries. Since I don't think there is a way to avoid the captcha from appearing 100% of the times, the question of how to click the buttons using Selenium's find_element_by_xpath() function remains the same. Leaving this as a note just in case someone finds it useful.
Ever tried to use the following function:
add_argument("-auto-open-devtools-for-tabs")
I managed to interact with captcha
If the position is always fixed, you can use PyAutoGUI to move the mouse and click on it
import pyautogui
pyautogui.click(100, 100) # button coordinates
Since, it is in iframe, we need to move our selenium pointing to iframe and then use your xpath.
driver.switch_to.frame("c-la7g7xqfbit4")
capchaBtn = driver.find_element_by_xpath("(//button[#id='recaptcha-audio-button'])[2]")

How to find element <label for="terms"> Python, selenium

I'm trying to click this button on this website. To get to it you start on https://fantasy5.com/football and once on the website you click on the login/signup button top right. then you are on the correct link and the button I am trying to press is the terms and privacy button in the signup section.
Any advice on what code I should use to find it would be much appreciated.
If you are trying to select that checkbox, use this xpath - //div[contains(#class,'Checkbox')][1]. Below line worked for me.
driver.find_element_by_xpath("//div[contains(#class,'Checkbox')][1]").click()
we have unique id terms
did you try :
driver.find_element_by_id("terms").click()

Is it possible to pick a specific button, that is on a page multiple times with selenium?

I am trying to code a program for somebody, that is possible to send and delete messages at the same time in the browser Discord application.(I think he wants to use this to spam?) He told me to write the code in Python, so I used python with Selenium. At this point I got everything working so far, the login works, the channel select works if you just insert the channel link directly. It also managed to let it send some messages. But problem now is, that I don't know how to delete the message after it was send, beacuse you need to click a "More" button first. But this more Button
I would use Webbot for that:
from webbot import Browser
web = Browser()
web.click('More')
that module has quite a few useful simple functions so i encourage you to try it

Sometimes not sending full text to input as expected by sendkeys in selenium python

I try to login a site automatically by selenium.
My script sometimes works smoothly but sometimes it does not send full-text of user name or passwork to relavant inputs so it then failed to login.
I try to insert implicitly_wait but it appears not to solve this problem totally.
I wonder if there is any function to require the webdriver to fullfill each of input before excecute another one in selenium.
Here is may attempt:
browserdriver.get(url)
#fill username
browserdriver.implicitly_wait(25)
psusername=browserdriver.find_element_by_xpath("//*[#class='form-group'][1]/input[#name='username']")
psusername.click()
psusername.send_keys("1234567890")
#fill password
browserdriver.implicitly_wait(25)
pspass=browserdriver.find_element_by_xpath("//*[#class='form-group'][2]/input[#name='password']")
pspass.click()
pspass.send_keys("abcdefgfk12345")
browserdriver.implicitly_wait(25)
pssubmit=browserdriver.find_element_by_xpath("//*[#class='login-btn']/button[#type='submit']")
pssubmit.click()
Pls, explain for me why I get stuck on this problem and how to solve it! thanks alot
EDIT1: I add the target site address: click here You should click on login words (Đăng nhập) on the top right screen to see the popup login pannel.
EDIT 2: Thank to guide of below comments, I try to add some code lines to force it to key individual letter and it appears to work well so I add solution here:
text_input="1234567890"
for i in text_input:
psusername.send_keys(i)
Use set attribute instead of sendkeys
here is the example code:
webdriver.executeScript("document.getElementById('elementID').setAttribute('value', '1234567890')");

Categories