Hi I have been trying to automate a page using Selenium Python on MACOS. And in one of the page there is a file picker popup that is supposed be displayed to upload files. Manually when running I get to see the file picker popup but when running via script the file picker popup is not at all getting displayed nn clicking the Upload Answer Button.
Following is the code that I tried or am using
from selenium.webdriver import Chrome
from random import shuffle
from time import sleep
from moviepy.editor import VideoFileClip
from selenium.common.exceptions import ElementClickInterceptedException
import math
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
#To Enable the notification (Allow Microphone and Camera)
option = Options()
# option.add_argument("--enable-infobars")
# option.add_argument("start-maximized")
# option.add_argument("--enable-extensions")
# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", {
"profile.default_content_setting_values.media_stream_mic": 2,
"profile.default_content_setting_values.media_stream_camera": 2,
"profile.default_content_setting_values.geolocation": 1,
"profile.default_content_setting_values.notifications": 1
})
driver = Chrome(options=option, executable_path='/Users/downloads/chromedriver')
driver.get('https://study.yupl.us/studies/5d5a64aa8b5bb92fb776753b')
#click Start Study
sleep(5)
try:
element = driver.find_element_by_xpath("//button[text()='START STUDY']")
driver.execute_script("arguments[0].click();", element)
except ElementClickInterceptedException:
pass
#Enter the emailid
driver.find_element_by_name('email').send_keys('5d5a64aa8b5bb92fb776753b__relva@youplus.co')
#Enter the PASSWORD
password=driver.find_element_by_name('password')
password.send_keys('testtest')
password.send_keys(Keys.RETURN)
#click Start Study
sleep(5)
try:
element = driver.find_element_by_xpath("//button[text()='START STUDY']")
driver.execute_script("arguments[0].click();", element)
except ElementClickInterceptedException:
pass
#Function to get the VIDEO DURATION
def GetVideoDuration(video_element):
val = VideoFileClip(video_element)
return val.duration
#Get The video duration and wait for the video to completly play
videoduration=0
try:
sleep(10)
questionvideo = driver.find_element_by_id('studyMediaVideo')
video = driver.execute_script("return arguments[0].getAttribute('src')", questionvideo)
videoduration = GetVideoDuration(video)
# Play the video
driver.execute_script("arguments[0].click();", questionvideo)
except ElementClickInterceptedException:
pass
#wait till the video is fully played
sleep(math.ceil(videoduration))
#Click on Answer Question
try:
answer=driver.find_element_by_xpath('//button[text()="ANSWER"]')
driver.execute_script("arguments[0].click();", answer)
except ElementClickInterceptedException:
pass
#Click on Upload Answer
try:
sleep(3)
upload_answer=driver.find_element_by_xpath('//button[text()="UPLOAD ANSWER"]')
driver.execute_script("arguments[0].click();", upload_answer)
print('clicked on upload answers button')
except ElementClickInterceptedException:
pass
In Actual when the Answer Button is click. File picker popup is supposed to be displayed. Instead no popup or error messages are shown.
I have also tried to upload the file using the below code on that button
METHOD #1
driver.find_element_by_xpath('//button[text()="UPLOAD ANSWER"]').send_keys(os.getcwd() + "/Users/Downloads/samsung_videos/video.mp4")
METHOD #2
driver.find_element_by_xpath('//button[text()="UPLOAD ANSWER"]').send_keys("/Users/Downloads/samsung_videos/video.mp4")
And Below is the HTML code for that element
<button class="ui button" role="button" style="background-color: rgb(31, 186, 238); color: white; border-radius: 0px; padding-left: 40px; padding-right: 40px; font-family: "Open Sans"; font-size: 17px; font-weight: 600; line-height: 20px; text-align: center; width: inherit; height: 50px; margin-top: 26px;">UPLOAD ANSWER</button>