Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all 98906 articles
Browse latest View live

How to install selenium for python2.7

$
0
0

I've been asked to help a friend with a script health.py which uses python 2 and selenium.

I've been using python 3 and have selenium downloaded and working with 3.

When I try run the script with python 2 I get:from selenium import webdriverImportError: No module named selenium

If I try install:

pip install selenium

Then it says:

Requirement already satisfied: selenium in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (3.141.0)Requirement already satisfied: urllib3 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from selenium) (1.25.11)

I notice that those paths both point to python3.6 so how do I install selenium in my python 2.7 location?

Sadly I've always been a bit confused with how these python paths specifically work so have probably just made a mess in previous acts. If it helps here is this:

which python2.7/usr/bin/python2.7

I also tried reisntalling python2 with brew

brew install python@2

hoping this would reset my settings and allow another pip install of selenium to put things in the correct place, but nothing seemed to change.

Thanks for any help!

edit - extra info: I've noticed that my python3 is inside /Library while my python 2 is inside /System. Could this be a source of my issues?

/Library/Frameworks/Python.framework/Versions/3.6/System/Library/Frameworks/Python.framework/Versions/2.7

How to manually pass a captcha on a page opened in selenium [closed]

$
0
0

I read that there is a way to open a window through selenium and pass the captcha manually. I open it like this:

options = Options()options.add_argument('--headless')options.add_argument('--no-sandbox')driver = webdriver.Chrome(executable_path = "/usr/bin/chromedriver", options = options)driver.maximize_window()driver.get(url_rec)driver.close()driver.quit()

The link opens there inside. And how to see the real window to pass the captcha

Tips and suggestions for a Scrapy/Selenium Project?

$
0
0

I am building a price monitor with Python and Scrapy and since I'm new to Scrapy, I'm looking for tips and suggestions on how to better structure my project. So far my idea is as follows:

  1. I have a Google Sheet with a list of products code and respective store to monitor the price of each product;
  2. From there, I have a script that pulls all the information;
  3. I will use Selenium to browse to the store, search each product by its code and retrieve a list of URL of each given product in the desired store;
  4. Then, using Scrapy, I will start a spider and give it the list of URLs to retrieve the price, review rating, and more;
  5. This retrieved info will then be appended to a list of dictionaries;
  6. Finally, this list will be used to populate another worksheet in the same Google Sheet as a report with date and other extra info because this script will be run weekly.

I have a few questions:

a) Is there a better option than Selenium to search for the product and get the URL?
b) Should I use Scrapy or keep with Selenium for web scraping? I'm trying to diversify my knowledge but also looking for the best option.
c) What is the best practice for handling the data from Scrapy? Save it all in a .json and read it later when updating the sheet? Or, save each item's data after every link request? (I'm not sure if that's possible with Scrapy when you give the spider a list of URLs to follow)

Get text from button with selenium

$
0
0

I have this element from a webpage

<button class="lmt__translations_as_text__text_btn">How are you</button>

and trying to get the "How are you" with selenium. Somebody that knows a solution for that?

I have tried to use .text and .getAttribute but I always get no output.

Scroll Within a Div in Selenium with Python

$
0
0

I need Selenium to click on an element in a div, but it keeps saying that the click is "intercepted." This is because the div scrolls down and the element I want it to click on requires scrolling down the div to become visible. How do I get Selenium to scroll to a specific point/element (I have its XPath to identify it) within that div?

Here's a screenshot from the page in question (I've redacted certain elements for privacy):

Screenshot

How to send text to input field

$
0
0
driver.get("https://beta.stress.pw/login")enter_searchbar.element = driver.find_element_by_xpath=("/html/body/div[1]/div/div[2]/form/div/div[2]/input[1]")enter_searchbar.send_keys("itartinas1")

Error I get:

Exception has occurred: StaleElementReferenceException Message: staleelement reference: element is not attached to the page document
(Session info: chrome=86.0.4240.198)

Can you query a page's Redux state when scraping with Selenium?

$
0
0

I found some data in a webpages redux state and am wondering is there a way to use selenium to scrape the pages redux state? Thanks for any insight!

Selenium Chrome Webdriver not working in headless mode with profile

$
0
0

So, this is the code I'm having troubles with:

def scrap():        options = webdriver.ChromeOptions();        options.add_argument('headless');        options.add_argument('--profile-directory=Profile 1')        options.add_argument("--user-data-dir=C:/Users/omarl/AppData/Local/Google/Chrome/User Data/")        options.add_argument("--remote-debugging-port=45447")        options.add_argument("--disable-gpu")         browser = webdriver.Chrome(executable_path=r"C:\Users\omarl\OneDrive\Escritorio\chromedriver.exe", options=options)        scrapURL = "https://es.wallapop.com/search?distance=30000&keywords=leggins&latitude=41.38804&longitude=2.17001&filters_source=quick_filters"        browser.get(scrapURL)        #...

And the error:

WebDriverException: unknown error: unable to discover open pages

I don't have any instances of chrome when I execute the script, and when I'm using it without the headless option it works fine. Any idea why this is happening? Please, note that I'm using the --remote-debuggin-port provided in similar questions.

I'm using ChromeDriver 86.0.4240.22


What is the correct syntax of XPath for svg and svg child elements? [duplicate]

$
0
0

I copied XPath for a SVG element in Chrome Devtools and when I verified that in devtools it is showing 0 matching node and it has syntax like /svg

For same element, the XPath which I got from SelectorsHub is showing 1 matching node with the syntax like //*[local-name()='svg']

Can someone, please help me which is the correct syntax and whether devtools is giving correct xpath or SelectorsHub?

Selenium webdriver - execute_script on the browser that I'm currently in

$
0
0

so I'm running some tests with Robot Framework in a Firefox webdriver.During these tests, I need to do something like this:

from selenium import webdriver from robot.api.deco import keywordfrom robot.libraries.BuiltIn import BuiltInfrom SeleniumLibrary import SeleniumLibrary@keyword('Refresh Page Menu')def refresh_page():        # create webdriver object         driver = webdriver.Firefox()         driver.get("https://mytest.html")         # write script         script = "navigateTodiagram(305)"       # generate a alert via javascript         driver.execute_script(script) 

the thing is how can I do this with the web that I'm currently doing my tests on Robot Framework.Instead of opening a new browser?I want to execute_script("navigateTodiagram(305)") on the browser where I'm doing my tests.

Anyone has any ideas?Thank you very much

Is there a way to extract headers using selenium?

$
0
0

I have a test where I navigate to a site and I want to get the headers of the response.Is there a way I can do it using selenium?

    URL obj = new URL(url);    URLConnection conn = obj.openConnection();    Map<String, List<String>> map = conn.getHeaderFields();    for (Map.Entry<String, List<String>> entry : map.entrySet()) {        Log.info(String.format("key is %s and value is %s", entry.getKey(),         entry.getValue()));    }

I tried this code but it's not working very well since i already used driver.get()

Thanks

How to wait for an element to be visible in Selenium newer versions, using Java?

$
0
0

I started learning Selenium lately and i wrote a simple code which uses Selenium 3.2.2, and waits for an email field to load in order to fill it:

     WebDriverWait wait=new WebDriverWait(driver,25);    WebElement login = driver.findElement(By.xpath("//*[@id=\"Login\"]/div/div/button[1]"));        login.click();        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"email\"]")));

Ive updated to a new Selenium version and since then I cant get it to work- the last row now resuqires a function, how can i write a function that waits for an web element to be loaded? thanks

Python Selenium Element Not Found

$
0
0

When element not found should print : not found, but i got error :

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name='q']"}

Here is my script and the element 'search' doesn't exist

import timefrom selenium import webdriverdriver = webdriver.Chrome(executable_path=r"C:\Users\SAMSUNG\chromedriver.exe")driver.get('https://facebook.com')search = driver.find_element_by_xpath("//input[@name='q']")time.sleep(15)if search:    time.sleep(15)    print('found')    driver.find_element_by_xpath("//input[@name='q']").send_keys('test')else:    print('not found')    pass

Didn't work because we can't do len to element ( we can do it only to elements with an s )

Inconsistency with selenium driver.find_element

$
0
0

I am trying to input a password into a box using Python. This is the HTML part of the webpage that includes the box to type in.

<input type="password" placeholder="" name="password" id="input6" value="" aria-label="" autocomplete="off" class="password-with-toggle">

My code is this:

pass_box = driver.find_element(By.ID, 'input6')pass_box.send_keys(password)

It works every once in a while but is not consistent at all. The HTML does not change. Putting in the username and clicking the next button works, but will throw an error every now and then when it comes to the password box. The error says unable to locate element.

Selecting bootstrap combobox value through selenium

$
0
0

I am trying to select a combobox value through selenium. This is not a usual combobox. It's something with bootstrap.

element = DriverFactory.getDriver().findElement(By.xpath("/html/body/div[8]/div[2]/div[3]/div[1]/div[1]/div[2]/div/div"));executor.executeScript("arguments[0].click();", element);

Then find element through xpath of the desired value and click it. But I want to make it bit generic like below:

element = DriverFactory.getDriver().findElement(By.xpath("/html/body/div\[8\]/div\[2\]/div\[3\]/div\[1\]/div\[1\]/div\[2\]/div/div"));executor.executeScript("arguments\[0\].click();", element); List<WebElement> options = element.findElements(By.xpath("//div\[@class='awd_faxdelivery_coversheet_dropdown'\]div/div/ul/li"));for (WebElement opt : options) {​​​​    System.out.println(options);    if (opt.getText().equals("APP")) {​​​​        executor.executeScript("arguments\[0\].click();", opt);        return;    }​​​​}

attached are HTML and UI images:formdom


What is the best way to include the below code to testing Framework [closed]

$
0
0

this code contains both the main class and sub class/s. looking for a proper framework example since I am new to codes

'''package excelExportAndFileIO;

public class ReadExcelFile {

public void readExcel(String filePath,String fileName,String sheetName) throws IOException{    File file =    new File(filePath+"\\"+fileName);    FileInputStream inputStream = new FileInputStream(file);        Workbook WorkbookDoc = null;    String fileExtensionName = fileName.substring(fileName.indexOf("."));    if(fileExtensionName.equals(".xlsx")){        WorkbookDoc = new XSSFWorkbook(inputStream);    }    else if(fileExtensionName.equals(".xls")){          WorkbookDoc = new HSSFWorkbook(inputStream);    }      Sheet ExcelSheet = WorkbookDoc.getSheet(sheetName);    int rowCount = ExcelSheet.getLastRowNum()-ExcelSheet.getFirstRowNum();    for (int i = 0; i < rowCount+1; i++) {        Row row = ExcelSheet.getRow(i);        for (int j = 0; j < row.getLastCellNum(); j++) {              System.out.print(row.getCell(j).getStringCellValue()+"|| ");        }        System.out.println();    }     }      public static void main(String...strings) throws IOException{        ReadExcelFile objExcelFile = new ReadExcelFile();    String filePath = System.getProperty("user.dir")+"\\src\\excelExportAndFileIO";     objExcelFile.readExcel(filePath,"ExportExcel.xlsx","Sheet1");    }   

}'''

I'm trying to use selenium for python and it's returning a 'common exception' error

$
0
0

Disclaimer

This is my first time using stack overflow, and I'm relatively new to coding, so if I've missed any important info, or not done something 'properly' feel free to let me know.

The Problem

I've just installed selenium using pip3 install selenium which all went fine, I imported everything as I believe I'm supposed to.

import seleniumfrom selenium import webdriver

To test I then added default_browser = webdriver.Firefox() But unfortunately met this error:

Traceback (most recent call last):  File "/home/kali/Documents/coding/ProjectName/main.py", line 4, in <module>    default_browswer = webdriver.Firefox()  File "/home/kali/.local/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__    RemoteWebDriver.__init__(  File "/home/kali/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__    self.start_session(capabilities, browser_profile)  File "/home/kali/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session    response = self.execute(Command.NEW_SESSION, parameters)  File "/home/kali/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute    self.error_handler.check_response(response)  File "/home/kali/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response    raise exception_class(message, screen, stacktrace)selenium.common.exceptions.WebDriverException: Message: newSession

I've tried following the steps from this stack overflow question, including:

  1. Trying to update selenium using pip3 install -U selenium AND sudo pip3 install selenium --upgrade
  2. I've upgraded geckodriver by following the steps 1-3 from here, and then replacing step 4 with sudo mv geckodriver /usr/local/bin/geckodriver
  3. Troubleshooting geckodriver using multiple ports by running: `strace geckodriver 2>&1 | grep -iE 'bind|getsockname' which returned
bind(3, {sa_family=AF_INET, sin_port=htons(4444), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EADDRINUSE (Address already in use)

I then ran netstat -tulpn | grep -i 4444 which returned:

tcp        0      0 127.0.0.1:4444          0.0.0.0:*               LISTEN      7500/geckodriver   

This apparently meant that I need to let the OS allocate a free port:geckodriver --port 0 which returned:

1605300277587   geckodriver INFO    geckodriver 0.19.11605300277592   geckodriver INFO    Listening on 127.0.0.1:37399

After this the terminal continues to run and doesn't give me back my xyz:~$: thing.

None of this has made any difference whatsoever to my issue, and I can't really find any other resources that are related, so if anyone knows what I could try, I'd appreciate it.

How to get the Browser info in C# WebDriver?

$
0
0

I see a ICapabilities interface to get the Browser info;Did couple of googling with no luck for any code example; Can anybody please share anything how I can get the browser info for a particular IWebDriver instance ? I am using C# webdriver.

Testing streaming service (like selenium) [closed]

$
0
0

Good afternoon.

I'm testing my company's streaming service, which works like twitch.

The task is as follows:Log in to your account and simulate viewing the stream ( and chat)

I was thinking of writing code in selenium. But as far as I understand, in this case you will have to use your own driver for each thread. I'm afraid it will take up too much memory.

Now the question.It's true? Is there a way to avoid this?What methods would you recommend to solve this problem?

I just came up with the idea to try not drawing videos to save resources. But there is one caveat here, so that the streaming service doesn't think I'm a bot.In other words, I have to constantly get it, but not draw it.This won't work with selenium.

The question is as follows: is it possibleto send login data to the form and "view" the stream programmatically in Java?Which libraries should I use?

How to see "Network" tab in selenium? [Python]

$
0
0

There is a Network tab in DevTools, so how do I see that with selenium?
Network tab image in Chrome

Thanks

Viewing all 98906 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>