Categories
Python Answers

How to find an element that contains specific text in Selenium WebDriver with Python?

Sometimes, we want to find an element that contains specific text in Selenium WebDriver with Python.

In this article, we’ll look at how to find an element that contains specific text in Selenium WebDriver with Python.

How to find an element that contains specific text in Selenium WebDriver with Python?

To find an element that contains specific text in Selenium WebDriver with Python, we can use the find_elements_by_xpath method.

For instance, we write

driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")

to call find_elements_by_xpath with "//*[contains(text(), 'My Button')]" to find the elements that has text My Button.

Conclusion

To find an element that contains specific text in Selenium WebDriver with Python, we can use the find_elements_by_xpath method.

Categories
Python Answers

How to remove duplicate columns with Python Pandas?

Sometimes, we want to remove duplicate columns with Python Pandas.

In this article, we’ll look at how to remove duplicate columns with Python Pandas.

How to remove duplicate columns with Python Pandas?

To remove duplicate columns with Python Pandas, we can use ~ with the df.columns.duplicated method.

For instance, we write

df = df.loc[:,~df.columns.duplicated()]

to remove columns based on duplicate column names by slicing the data frame with

df.loc[:,~df.columns.duplicated()]

We use it to return all the columns that have unique column names and assign the returned data frame back to df.

Conclusion

To remove duplicate columns with Python Pandas, we can use ~ with the df.columns.duplicated method.

Categories
Python Answers

How to use XPath in Python?

Sometimes, we want to use XPath in Python.

In this article, we’ll look at how to use XPath in Python.

How to use XPath in Python?

To use XPath in Python, we can use the xml.etree.ElementTree class.

For instance, we write

import xml.etree.ElementTree as ET
root = ET.parse(filename)
result = ''

for elem in root.findall('.//child/grandchild'):
    if elem.attrib.get('name') == 'foo':
        result = elem.text
        break

to parse the XML file at the filename path with parse.

And then we call root.findall with an XPath to the elements we want.

Then we loop through the returned elements with a for loop.

In the loop we use attrib.get to get the attribute values by its name.

And we get the text content of the element with text.

Conclusion

To use XPath in Python, we can use the xml.etree.ElementTree class.

Categories
Python Answers

How to find all the factors of a number in Python?

Sometimes, we want to find all the factors of a number in Python.

In this article, we’ll look at how to find all the factors of a number in Python.

How to find all the factors of a number in Python?

To find all the factors of a number in Python, we can use the reduce function.

For instance, we write

from functools import reduce

def factors(n):    
    return set(reduce(list.__add__, 
                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

to define the factor function that returns the factors by looping from 1 to int(n**0.5) + 1 with a for loop.

And we put the values n that’s evenly divisible by i with

n % i == 0

And we use list.__add__ to add the factors into a list.

Finally, we call set to convert the returned list without the duplicate values by converting the list to a set.

Conclusion

To find all the factors of a number in Python, we can use the reduce function.

Categories
Python Answers

How to correctly clean up a Python object?

Sometimes, we want to correctly clean up a Python object.

In this article, we’ll look at how to correctly clean up a Python object.

How to correctly clean up a Python object?

To correctly clean up a Python object, we can use a with statement.

For instance, we write

class Package:
    def __init__(self):
        self.files = []

    def __enter__(self):
        return self

    # ...

    def __exit__(self, exc_type, exc_value, traceback):
        for file in self.files:
            os.unlink(file)

to add the __enter__ and __exit__ methods in the code to return the class instance and run clean up code respectively.

And then we can use it with

with Package() as package_obj:
    # ...

to create a Package instance and assign the instance returned by __enter__ to package_obj.

Then once we’re done running the code in with, __exit__ will be run to clean up automatically.

Conclusion

To correctly clean up a Python object, we can use a with statement.