Categories
Python Answers

How to get HTML source of WebElement in Selenium WebDriver using Python?

Sometimes, we want to get HTML source of WebElement in Selenium WebDriver using Python.

In this article, we’ll look at how to get HTML source of WebElement in Selenium WebDriver using Python.

How to get HTML source of WebElement in Selenium WebDriver using Python?

To get HTML source of WebElement in Selenium WebDriver using Python, we can call the get_attribute method.

For instance, we write

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

# ...

element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#my-id")))
print(element.get_attribute("outerHTML"))

to get get the element with ID my-id with

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#my-id")))

Then we get the element’s outerHTML property with

element.get_attribute("outerHTML")

to get the returned element‘s HTML.

Conclusion

To get HTML source of WebElement in Selenium WebDriver using Python, we can call the get_attribute method.

Categories
Python Answers

How to do fuzzy string comparison with Python?

Sometimes, we want to do fuzzy string comparison with Python.

In this article, we’ll look at how to do fuzzy string comparison with Python.

How to do fuzzy string comparison with Python?

To do fuzzy string comparison with Python, we can use difflib.

For instance, we write

import difflib

matches = difflib.get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])

to call difflib.get_close_matches with the search string and an array of possible matches.

get_close_matches returns a list of close matches from the strings in the list in the 2nd argument.

We can also call get_close_matches with keyword.kwlist by writing

import difflib
import keyword

matches = get_close_matches('wheel', keyword.kwlist)

keyword.kwlist is the list of keywords to search in to get close matches.

Concluson

To do fuzzy string comparison with Python, we can use difflib.

Categories
Python Answers

How to check if the string is empty with Python?

Sometimes, we want to check if the string is empty with Python.

In this article, we’ll look at how to check if the string is empty with Python.

How to check if the string is empty with Python?

To check if the string is empty with Python, we can use the not operator.

For instance, we write

if not my_string:
  # ...

to check my_string is empty with if not my_string.

Empty strings are falsy so we can use not to check if the string is empty.

Conclusion

To check if the string is empty with Python, we can use the not operator.

Categories
Python Answers

How to redirect to a new URL with Python Requests library?

Sometimes, we want to redirect to a new URL with Python Requests library.

In this article, we’ll look at how to redirect to a new URL with Python Requests library.

How to redirect to a new URL with Python Requests library?

To redirect to a new URL with Python Requests library, we can get the redirect URL with the response object’s url property.

For instance, we write

import requests

r = requests.get('http://www.github.com')
print(r.url)

to call requests.get with a URL.

Then we get the URL that the URL that we make the request redirects to with r.url.

Conclusion

To redirect to a new URL with Python Requests library, we can get the redirect URL with the response object’s url property.

Categories
Python Answers

How to join a list of items with different types as string in Python?

Sometimes, we want to join a list of items with different types as string in Python.

In this article, we’ll look at how to join a list of items with different types as string in Python.

How to join a list of items with different types as string in Python?

To join a list of items with different types as string in Python, we convert all the items in the list into strings before calling join.

For instance, we write

s = ', '.join(map(str, my_list))

to call map with str and my_list to return an iterable with all the items in my_list converted to strings.

Then we call ', '.join with the iterable to join the strings with ', ' and return it.

Conclusion

To join a list of items with different types as string in Python, we convert all the items in the list into strings before calling join.