Posts

Showing posts with the label Selenium

Anaconda Selenium And Chrome

Answer : The easiest would be to install chrome-driver via anaconda (especially when running on a machine where you don't have permissions to install chrome-driver from .deb package) conda install -c conda-forge python-chromedriver-binary (updated based on comment from bgoodr (https://stackoverflow.com/users/257924/bgoodr) - please vote his comment below ). The simplest solution is to install chromedriver as suggested by @bgodr: conda install -c conda-forge python-chromedriver-binary Then at the top of your code, add the following import statement to update your PATH variable appropriately: import chromedriver_binary Download latest chromedriver Update Chrome itself In your code from selenium import webdriver driver_path = '/path to chromedriver.exe/' driver = webdriver.Chrome(driver_path) driver.get('somewebsite')

Checking If Element Exists With Python Selenium

Answer : a) from selenium.common.exceptions import NoSuchElementException def check_exists_by_xpath(xpath): try: webdriver.find_element_by_xpath(xpath) except NoSuchElementException: return False return True b) use xpath - the most reliable. Moreover you can take the xpath as a standard throughout all your scripts and create functions as above mentions for universal use. UPDATE : I wrote the initial answer over 4 years ago and at the time I thought xpath would be the best option. Now I recommend to use css selectors . I still recommend not to mix/use "by id", "by name" and etc and use one single approach instead. None of the solutions provided seemed at all easiest to me, so I'd like to add my own way. Basically, you get the list of the elements instead of just the element and then count the results; if it's zero, then it doesn't exist. Example: if driver.find_elements_by_css_selector('#element')...

Change User-agent For Selenium Web-driver

Answer : There is no way in Selenium to read the request or response headers. You could do it by instructing your browser to connect through a proxy that records this kind of information. Setting the User Agent in Firefox The usual way to change the user agent for Firefox is to set the variable "general.useragent.override" in your Firefox profile. Note that this is independent from Selenium. You can direct Selenium to use a profile different from the default one, like this: from selenium import webdriver profile = webdriver.FirefoxProfile() profile.set_preference("general.useragent.override", "whatever you want") driver = webdriver.Firefox(profile) Setting the User Agent in Chrome With Chrome, what you want to do is use the user-agent command line option. Again, this is not a Selenium thing. You can invoke Chrome at the command line with chrome --user-agent=foo to set the agent to the value foo . With Selenium you set it like this: fr...

ChromeDriver - Disable Developer Mode Extensions Pop Up On Selenium WebDriver Automation

Answer : Did you try disabling the developer extensions with command line param? Try with the following Selenium WebDriver java code: System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("--disable-extensions"); driver = new ChromeDriver(options); I cannot disable extensions because I'm developing & testing one. What I'm doing to dismiss this popup is the following: I load chrome with my extension using Selenium. I then immediately create a new window (via the SendKeys(Control-N) method). This predictably brings up the "Disable Developer Mode Extensions" popup after 3 seconds in the new window. I can't tell programmatically when it pops up (doesn't show in screenshots) so instead I simply wait 4 seconds. Then I close the tab via driver.Close(); (which also closes this new window). Chrome takes that as "cancel", dis...

Click Button By Text Using Python And Selenium

Answer : You can find all buttons by text and then execute click() method for each button in a for loop. Using this SO answer it would be something like this: buttons = driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]") for btn in buttons: btn.click() I also recommend you take a look at Splinter which is a nice wrapper for Selenium. Splinter is an abstraction layer on top of existing browser automation tools such as Selenium, PhantomJS and zope.testbrowser. It has a high-level API that makes it easy to write automated tests of web applications. I had the following in html: driver.find_element_by_xpath('//button[contains(text(), "HELLO")]').click()