简体   繁体   中英

How to click on Cancel on a windows explorer that displays when a upload button is clicked on a web page using Selenium WebDriver with Java

I am very new to Selenium WebDriver with Java. There is a Upload button on a job portal. When I click on that button windows explorer is displayed to choose the file. there are open and cancel buttons on this window. i want to select the cancel button. since it is a windows explorer i cannot inspect the cancel button. how do we write the code for cancelling the button. Thanks in advance.

driver.get("https://my.indeed.com/resume?from=gnav-homepage&co=US&hl=en_US");

driver.manage().window().maximize();

Thread.sleep(3000);

driver.findElement(

By.xpath("//[@id='container']/div/div/div[2]/div/div/div[2]/div/div[1]/div/div[1]/button")).click();

问题截图

I do not have Java programming skills. I hope the below python code is easy to translate. In my opinion, combining windows action with selenium calls is often flaky.

There is a hidden element called 'upload resume button', you can change the attribute value to see it on the UI, use the send keys method on the element to upload the resume.

 from selenium.webdriver import Remote, DesiredCapabilities

 driver = Remote(desired_capabilities=DesiredCapabilities.CHROME)
 driver.get('https://my.indeed.com/resume?from=gnav-homepage&co=US&hl=en_US')

 driver.execute_script(
     "document.getElementById('upload-resume-button').setAttribute('class', '')"
 )

upload_your_resume = driver.find_element_by_id('upload-resume-button')
upload_your_resume.send_keys(r'C:\test\resume.docx')

The above code worked in my local.

Short answer is you cant, but if you want to upload a local file you can use send_keys on the input field, sending the file path.

Here is a python exemple:

driver.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")

you can use Robot class to simulate native keyboard and mouse actions to interact with windows based pop-ups. The shortcut to close any opened window is: “Alt + Space +C” – Closes the focused window.

Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_ALT); 
rb.keyPress(KeyEvent.VK_SPACE);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_SPACE);
rb.keyRelease(KeyEvent.VK_ALT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM