0

I have searched through the forums and have come upon nothing covering this question. I am looking to add a noise alert in selenium, python webdriver when I find a certain X path. (Like a buzzer or a beep) I have only found info pertaining to pop up alerts when I search for alerts. What I would like to do is have a noise alert when I find a certain link

Driver.get(“google.com”)
Driver.find_element_by_text(“search button”).click()

I want to add an alert when this is clicked for example

bad_coder
  • 5,829
  • 13
  • 26
  • 41
  • This may be helpful: https://stackoverflow.com/questions/16573051/sound-alarm-when-code-finishes – JD2775 Mar 10 '21 at 00:06

1 Answers1

0

You can try this (assuming you are on Windows):

import winsound
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

duration = 1000  # milliseconds
freq = 440  # Hz

Driver.get(“google.com”)
element = driver.find_element_by_name('q')
if element.is_displayed():
    winsound.Beep(freq, duration)
    element.send_keys("Python")
    element.send_keys(Keys.ENTER)
    print("found")
else:
    print("not found")
JD2775
  • 2,810
  • 5
  • 18
  • 36
  • if element.is_displayed(): AttributeError: 'NoneType' object has no attribute 'is_displayed' – Sauls_bHlls Mar 10 '21 at 00:38
  • I was assuming your original element was correct, it was not, assuming you are trying to click the search button. I updated the code. BTW you may want to add code to enter some text in the search box first before clicking the button – JD2775 Mar 10 '21 at 00:41
  • `import winsound duration = 1000 # milliseconds freq = 440 # Hz driver.get("https://www.google.ca/?gws_rd=ssl") time.sleep(5) element = driver.find_element_by_xpath("/html/body/div[1]/div[3]/form/div[2]/div[1]/div[3]/center/input[2]").click() ` it works up until i add if element is displayed then it gives that error – Sauls_bHlls Mar 10 '21 at 01:19
  • still getting that error, im not sure whats wrong – Sauls_bHlls Mar 10 '21 at 02:23
  • Ok, I think I know what the issue is. When google.com loads that button is hidden for some reason, but the search box is not. I changed the code above for testing purposes, to look for the search box instead of the search button. This is just for testing purposes, you will want to add error handling etc to your code – JD2775 Mar 10 '21 at 02:48
  • Okay so just to get it clear, we are loading google and looking for the letter ‘q’. If it is not found we will get a print message saying it’s not there.. but if it is there we will get windows alert noise etc.. have you tried this code yet? I will try tomorrow in the morning . Appreciate you helping me – Sauls_bHlls Mar 10 '21 at 04:29
  • @Sauls_bHlls. Correct, you are looking for the element 'q', which is the search text box element. if its there you get the print message (and hopefully alert). I have tested the print function, but not the winsound function, since I work on a Mac and that is Windows specific. Hopefully it works ok for you. If you want a Mac version you can click on that initial link I commented on your post, I think there is one there – JD2775 Mar 10 '21 at 04:35