How to automate chrome extension that interacts with the parent webpage? - python

I want to automate the testing for a chrome-extension.
I have used selenium-python to automate the parent web-page , but i am unable to use selenium to automate the chrome-extension because selenium is bound to the web-page view,so i am not able to click the chrome extension icon present in the title bar of the google chrome. Due to this limitation, i tried using sikuli to click on the chrome-extension icon ,but i couldn't get the DOM of the chrome-extension popup HTML that appear when the extension's icon is clicked, as i had to access the text displayed by the extension's pop up html page and then evaluated it.My test failed.
I thought i could use shift_to_window() but in vain,because its a pop up html, and hence i cant shift.If instead i use sikuli ,as the test cases increased , the amount of pictures needed to automate it will also increase, so it won't be feasible.
Sample Screenshot,
In this picture,the web-store page is the parent page and the small popup in the right top corner,is the pop-up i am talking about, that appear after clicking the chrome extension . So basically i have to interact with that popup HTML.

I didn't quite understand why you can't automate whatever webpage you have with Selenium and what is the limitation you are describing. Perhaps an example such as screenshot or even a link to the relevant webpage can help. Furthermore, I don't understand why do being unable to extract DOM from a page is a limitation while using Sikuli. Sikuli has nothing to gain from the DOM, it purely based on visual content of the screen.
I would attempt to handle the popup you are describing using using standard Selenium functionality so something like this:
driver.switchTo().alert().dismiss();
There are additional alert handling options so you can choose what suits you most.

Related

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]")

Selenium can't locate elements inside HTML doc within another HTML doc

I don't know what's exactly the thing that Amazon opens when you click the "buy now" button, I tryed to search on internet but couldn't find a clear answer. It could be a pop-up or an iframe, but they call it "popover" in their source code, which I have no clue what it is.
The point is, once you open this "popover", Selenium is unable to parse any part of that frame since it's a standalone separate HTML doc. I'm not sure if it's an iframe as I've never seen an iframe like that, so any guide I serached online about how to switch context using an Handler to an iframe doesn't work here. I couldn't find an ID, name, or anything significant to allow Selenium to parse the frame.
image showing the "popover"
If anyone has any clue how this HTML element is actually called and how to allow Selenium to parse it, please let me know, thanks.
I checked HTML of the page and here are my findings:
'Close' button is not in iFrame
Popup content itself is in iframe
See screenshot below:
What does it mean for you:
In order to click on the button 'Close' (e.g. by css selector button[class*='a-button-close']) you do not need to switch to any frames. If you are failing to click on the button - I would assume that you are missing selenium wait. There is a small delay after you clicking on "Buy Now" button and before popup actually loads - you need to explicitly wait for it to load. See https://selenium-python.readthedocs.io/waits.html for more details on selenium waits. I would recommend using visibility_of_element_located or visibility_of to wait for popup/button to appear (load) in your case.
If you did any operations in the popup itself (e.g. if you clicked on some links inside of it) - you most likely switched to the context of the iFrame with driver.switch_to.frame(iframe). In this case - you need to switch back to main context with driver.switch_to.default_content() in order to click on the 'Close' button (since it is outside of iframe).
I highly recommend learning runtime evaluate technique - it will save you hours on debugging your applications - see https://www.jetbrains.com/pycharm/guide/tips/evaluate-expression/
P.S. next time when asking question - please post your code (at least on high level) - it will help with localizing issue.

Is there any other way to handle Chrome's Print Preview window when automating with Selenium?

I am trying to automate the printing of a document obtained from a website.
When I click the Print button on the web page, Chrome's print-preview appears and I have to choose from the drop-box('Save as PDF' or 'Print').
I am using Selenium with Python and didn't find any way to access that Print-Preview window.
Is there any other way that this can be done?
I don't have code to show you right now. It takes in details that are confidential for the company.

How do I use lxml to interact with the page and pull up a menu to be scraped?

For reference, this is the page that I will use as an example. It is the one that best demonstrates what I am trying to accomplish. If you look at the page, there is a brands banner at the top of the screen. In the top right hand corner, there is a see all button which pulls up a menu. The data from this menu is not in the html, it is generated by the click of that button. Is there any way to have lxml perform the action of clicking that button and pulling up that menu?
I took a look at the network log. There does not appear to me that there is any file or url in there that would contain the data from that menu. I believe selenium does have this functionality, but I would prefer to not have to use only lxml.
lxml is a parser, so it cannot click button elements on the page. Unfortunately, using a tool like Selenium is what you need to do to accomplish this.
I know you mentioned looking at the network log. Usually in these cases it is best to try to find the endpoint and issue the request directly, but if you tried and can't find the request then use Selenium.

I can’t click add extension in Firefox

I can’t click the blue add extension in Firefox.
I am wanting to install an addon in Selenium Firefox. As far as I can tell the following CSS is changing:
.install-wrapper > div:nth-child(1) > div:nth-child(1)
However, Selenium just does nothing.
The Add button you have pictured is not part of the page. It's a browser specific dialog and cannot be clicked with Selenium. The quick way to tell this is to right-click on the dialog and see if you see the Inspect option in the context menu. If you don't see it, then it's not part of the page and isn't accessible using Selenium. You might try the keyboard shortcut, ALT+A.
Selenium can only automate Web based applications. Here, you are trying to automate an element in the WebBrower which is a stand alone application installed in your system. If I were using java to write the test script here, I would use selenium to automate the WebApplications and if I encounter any popups on the the Browser, I would try using java classes.

Categories