Categories
Python Answers

How to wait until page is loaded with Selenium WebDriver for Python?

Sometimes, we want to wait until page is loaded with Selenium WebDriver for Python.

In this article, we’ll look at how to wait until page is loaded with Selenium WebDriver for Python.

How to wait until page is loaded with Selenium WebDriver for Python?

To wait until page is loaded with Selenium WebDriver for Python, we can use the presence_of_element_located method.

For instance, we write

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print("Timed out waiting for page to load")

to open the page with the given URL with get.

And then we wait for the element with the given ID by calling presence_of_element_located with (By.ID, 'element_id').

Then we have

WebDriverWait(driver, timeout).until(element_present)

to wait until the element is present.

Conclusion

To wait until page is loaded with Selenium WebDriver for Python, we can use the presence_of_element_located method.

Categories
Python Answers

How to use subprocess.Popen to connect multiple processes by pipes with Python?

Sometimes, we want to use subprocess.Popen to connect multiple processes by pipes with Python.

In this article, we’ll look at how to use subprocess.Popen to connect multiple processes by pipes with Python.

How to use subprocess.Popen to connect multiple processes by pipes with Python?

To use subprocess.Popen to connect multiple processes by pipes with Python, we can use the subprocess.PIPE property.

For instance, we write

import subprocess

some_string = b'input_data'

sort_out = open('outfile.txt', 'wb', 0)
sort_in = subprocess.Popen('sort', stdin=subprocess.PIPE, stdout=sort_out).stdin
subprocess.Popen(['awk', '-f', 'script.awk'], stdout=sort_in, 
                 stdin=subprocess.PIPE).communicate(some_string)

to pipe the sort_out file’s content to sort with subprocess.PIPE.

And then we pipe the result from sort to awk with subprocess.PIPE.

Conclusion

To use subprocess.Popen to connect multiple processes by pipes with Python, we can use the subprocess.PIPE property.

Categories
Python Answers

How to delete a specific line in a file with Python?

Sometimes, we want to delete a specific line in a file with Python.

In this article, we’ll look at how to delete a specific line in a file with Python.

How to delete a specific line in a file with Python?

To delete a specific line in a file with Python, we can open the file with open.

For instance, we write

with open("yourfile.txt", "r") as f:
    lines = f.readlines()
with open("yourfile.txt", "w") as f:
    for line in lines:
        if line.strip("\n") != "nickname_to_delete":
            f.write(line)

to open yourfile.txt and read the file lines into an iterable with

with open("yourfile.txt", "r") as f:
    lines = f.readlines()

And then we loop through the lines and call f.write to write the new lines into a file with

with open("yourfile.txt", "w") as f:
    for line in lines:
        if line.strip("\n") != "nickname_to_delete":
            f.write(line)

The content in yourfile.txt will be overwritten, so anything other than ‘nickname_to_delete’ will be written into yourfile.txt.

Conclusion

To delete a specific line in a file with Python, we can open the file with open.

Categories
Python Answers

How to merge lists into a list of tuples with Python?

Sometimes, we want to merge lists into a list of tuples with Python.

In this article, we’ll look at how to merge lists into a list of tuples with Python.

How to merge lists into a list of tuples with Python?

To merge lists into a list of tuples with Python, we can use the list and zip functions.

For instance, we write

list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
zipped = list(zip(list_a, list_b))

to call zip with list_a and list_b to combine the entries in the 2 lists into an iterable of tuples with the items from each list in the same position.

Then we use list to convert the zipped iterable into a list.

Conclusion

To merge lists into a list of tuples with Python, we can use the list and zip functions.

Categories
Python Answers

How to convert a .py to .exe for Python?

Sometimes, we want to convert a .py to .exe for Python.

In this article, we’ll look at how to convert a .py to .exe for Python.

How to convert a .py to .exe for Python?

To convert a .py to .exe for Python, we can use the cx_Freeze and idna packages.

To install them, we .run

pip install cx_Freeze
pip install idna

Then we create a setup.py file to convert a script to an exe file by writing

from cx_Freeze import setup, Executable

base = None    

executables = [Executable("foo.py", base=base)]

packages = ["idna"]
options = {
    'build_exe': {    
        'packages':packages,
    },    
}

setup(
    name = "<any name>",
    options = options,
    version = "<any number>",
    description = '<any description>',
    executables = executables
)

We use

executables = [Executable("foo.py", base=base)]

convert foo.py into an executable.

And then we add some options in options.

Next, we call setup with some arguments for the info for our executable to convert foo.py into an exe.

Conclusion

To convert a .py to .exe for Python, we can use the cx_Freeze and idna packages.