Categories
Python Answers

How to run JavaScript in Selenium using Python?

Sometimes, we want to run JavaScript in Selenium using Python.

In this article, we’ll look at how to run JavaScript in Selenium using Python.

How to run JavaScript in Selenium using Python?

To run JavaScript in Selenium using Python, we can use the execute_script method.

For instance, we write

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://example.com")
driver.execute_script("document.getElementByElement('foo').click()")

to call driver.get to open the http://example.com page.

Then we call execute_script with "document.getElementByElement('foo').click()" to run "document.getElementByElement('foo').click()".

Conclusion

To run JavaScript in Selenium using Python, we can use the execute_script method.

Categories
Python Answers

How to get the return value of JavaScript code in Python Selenium?

Sometimes, we want to get the return value of JavaScript code in Python Selenium.

In this article, we’ll look at how to get the return value of JavaScript code in Python Selenium.

How to get the return value of JavaScript code in Python Selenium?

To get the return value of JavaScript code in Python Selenium, we get the return value of the execute_script method.

For instance, we write

from selenium import webdriver

wd = webdriver.Firefox()
wd.get("http://localhost/foo/bar")
val = wd.execute_script("return 5")

to call execute_script to run the "return 5" JavaScript code.

Then we get the returned value from val, which is assigned the return value of execute_script.

Therefore, val is 5.

Conclusion

To get the return value of JavaScript code in Python Selenium, we get the return value of the execute_script method.

Categories
Python Answers

How to solve cross origin resource sharing error with Flask and Python?

Sometimes, we want to solve cross origin resource sharing error with Flask and Python.

In this article, we’ll look at how to solve cross origin resource sharing error with Flask and Python.

How to solve cross origin resource sharing error with Flask and Python?

To solve cross origin resource sharing error with Flask and Python, we can use the flask-cors package.

To install it, we run

pip install -U flask-cors

Then we use it by writing

from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

@app.route("/")
def hello_world():
   return "Hello, cross-origin-world!"

to call CORS with app to enable CORS throughout the app.

Conclusion

To solve cross origin resource sharing error with Flask and Python, we can use the flask-cors package.

Categories
Python Answers

How to convert date to timestamp with JavaScript?

Sometimes, we want to convert date to timestamp with JavaScript.

In this article, we’ll look at how to convert date to timestamp with JavaScript.

How to convert date to timestamp with JavaScript?

To convert date to timestamp with JavaScript, we can split the date string with split.

And then we pass the parts into the Date constructor.

For instance, we write

const myDate = "26-02-2022";
const [date, month, year] = myDate.split("-");
const newDate = new Date(year, month - 1, date);
console.log(newDate.getTime());

to call myDate.split with '-' to split the date string by '-'.

Then we create a new Date object with the year, month, and date.

We subtract month by 1 before we pass it into Date since JavaScript date’s months starts ranges from 0 for January and ends with 11 for December.

And then we call getTime to get the timestamp.

Conclusion

To convert date to timestamp with JavaScript, we can split the date string with split.

And then we pass the parts into the Date constructor.

Categories
Python Answers

How to pretty print nested dictionaries with Python?

Sometimes, we want to pretty print nested dictionaries with Python.

In this article, we’ll look at how to pretty print nested dictionaries with Python.

How to pretty print nested dictionaries with Python?

To pretty print nested dictionaries with Python, we call json.dumps with the indent argument.

For instance, we write

import json

print(
    json.dumps(
        {"a": 2, "b": {"x": 3, "y": {"t1": 4, "t2": 5}}}, sort_keys=True, indent=4
    )
)

to print the JSON string returned by json.dumps.

We call it with indent set to 4 to indent by 4 spaces in each level.

Conclusion

To pretty print nested dictionaries with Python, we call json.dumps with the indent argument.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.