Unable to interact with Hebrew text using Selenium WebDriver with Python

Exploring Selenium Web Driver using Python with Chrome driver has been an interesting journey for me. I discovered that while I was able to make it click a button with English text, it failed to do so when the text was in Hebrew. To illustrate this issue, I tested it on Google(.co.il).

Clicking the Gmail button worked perfectly fine, but attempting to click on "תמונות" (which means photos in Hebrew) resulted in an error. The exception error couldn't even be written properly (UnicodeEncodeError: 'ascii' codec can't encode characters in position 86-91: ordinal not in range(128)).

Here is the code snippet I used:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
from selenium import webdriver

driver = webdriver.Chrome("C:\Users\User-PC\Desktop\chromedriver_win32\chromedriver.exe")
driver.get("http://www.google.co.il/")

linkslist = [u'תמונות']

try:
    button = driver.find_element_by_link_text(linkslist[0])
    driver.implicitly_wait(5)
    button.click()
    print(driver.current_url)
except Exception as er:
    print "Error: ", format(er)

driver.close()
driver.quit()

Answer №1

It was successful in my case

browser.load('http://www.bing.com/')
browser.find_element_by_link_text('Images').click()

Answer №2

There seems to be a small issue in your code block regarding the print statement. Please find below the corrected version of the code:

from selenium import webdriver
driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("http://www.google.co.il/")
linkslist = [u'תמונות']
try:
    button = driver.find_element_by_link_text(linkslist[0])
    driver.implicitly_wait(5)
    button.click()
    print(driver.current_url)
except Exception as er:
    print ("Error: ", format(er))
driver.close()
driver.quit()

The output displayed on my console is:

https://www.google.co.il/imghp?hl=iw&tab=wi
Process finished with exit code 0

Update:

According to your recent update, you encountered an error message:

print ("Error: ", format(er)) UnicodeEncodeError: 'ascii' codec can't encode characters in position 86-91: ordinal not in range(128)

This error occurs because your IDE may not support Unicode characters for printing exceptions. To address this, we can modify the line for printing the exception by converting it into readable utf-8 character format as shown below:

print ("Error: ", format(er)).encode('utf-8')

or

print ("Error: ", format(er).encode('utf-8'))

Please let me know which option works best for you.

Answer №3

At last, I found a resolution, I opted for driver.find_element_by_link_text(linkslist[count]).send_keys(Keys.ENTER) rather than simply using driver.find_elements_by_link_text("texthere").click()

Appreciate all the assistance, wishing you a fantastic week =)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Optimal method for extracting PyTables table with column names stored as variable names

I have a piece of code that fulfills my requirements, but it takes more than 10 seconds to run on a table with 200 variables and 64000 rows. Is there a faster way to create a variable namespace that matches the column names? strExec = "a = table[:]" for ...

How can the linewidth and facecolor of an AnchoredOffsetbox be adjusted?

Can you customize the appearance of an AnchoredOffsetbox in matplotlib? I've implemented it to display variables next to my plot, with the '=' symbols aligned vertically. It serves as an additional legend, but I'm unable to find a way ...

Python3: Restrict file access to current directory

Currently working on a Python project for a basic file server. I have a situation where the client provides the filename, which cannot be trusted. How can I validate if it actually matches a file within the current directory or any of its subdirectories? W ...

Python's Selenium WebDriver seems to be malfunctioning when using `driver.find_element(By.XPATH, '')` syntax

Hoping to retrieve the Full Name "Aamer Jamal" using Selenium WebDriver from the provided URL above. However, encountering an issue where a NoSuchElementException is thrown. `Check out the code snippet below: from selenium import webdriver from selenium.we ...

I'm having trouble finding the right xpath. Can you assist me with this?

https://i.stack.imgur.com/Y3wDn.png Can someone assist me with finding the correct xpath? ...

Lost important documents after attempting to transfer them using Python's shutil.move command

My source folder had 120 files that needed to be moved to a new directory (destination). The destination was determined by the function I created, using information from the filenames. Here's an example of the function: path ='/path/to/source& ...

Is there a way for me to retrieve the locator value of a webelement?

How can I retrieve the selector value used in a selenium webelement in javascript? Let's say I have the following object: var el = browser.driver.findElement(by.id('testEl')); I want to extract the text 'testEl' from this object ...

Python AssertionError: The argument passed to the write() function in GAE must be a

Currently, I am utilizing Sublime Text 2 as my primary editor to develop a brand new Google App Engine project. UPDATE: While testing this code via localhost, I encountered an issue when accessing the app on appspot: Status: 500 Internal Server Error ...

What steps should be taken to confirm the visibility of an imported PDF modal?

I need to confirm how to assert or verify if this screen is appearing. I'm not sure how to do this because the "modal" belongs to the browser, not the webpage. Is there a way to create an assertion for verifying this? https://i.stack.imgur.com/g0kH ...

Confused by the WebDriver error message?

I encountered a WebDriver exception while trying to run a Selenium code on a grid. Here are the details of the code: Chrome Version: 58.0.3029.110 (64-bit) Selenium server: 3.4.0 @Test public void Testgrid() throws MalformedURLException{ DesiredCapab ...

Guide: Implementing Xavier initialization for weights in Tensorflow 2.0

With the transition to TF 2.0, the contrib library was removed, taking away functionalities like tf.contrib.conv2d and tf.contrib.layers.variance_scaling_initializer. So, what would be the most effective way to implement Xavier initialization in TF 2.0 wit ...

The error message "net::ERR_NAME_NOT_RESOLVED" appeared while using Selenium in Python to scroll down a webpage

Currently, I am in the process of developing a Python scraper using Selenium to gather website information. However, I encountered an issue when trying to access a page that is not live, resulting in the error: unknown error: net::ERR_NAME_NOT_RESOLVED It ...

How can we transform the input for the prediction function into categorical data to make it more user-friendly?

To verify my code, I can use the following line: print(regressor.predict([[1, 0, 0, 90, 100]])) This will generate an output. The first 3 elements in the array correspond to morning, afternoon, and evening. For example: 1, 0, 0 is considered as morning 0 ...

Can someone help me with combining these two HTML and JavaScript files?

I successfully created an HTML/JavaScript file that functions properly: <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor ...

How to handle timeout events with Ruby Selenium framework

Is there a method in Ruby Selenium to capture all timeout error events? I am currently configuring Jenkins with Selenium, but I am unsure of the most effective way to stop building tasks between steps. The only solution I have come across so far is insert ...

Guide on incorporating a singular Python document (filelock) into PyInstaller using a specfile:

I am currently facing an issue with packaging my application using pyinstaller for Windows. I am trying to implement file locking using the filelock.py script, but it is proving to be a challenge due to its unconventional structure. My datas list in the s ...

Converting Python scripts into executable files using py2exe and utilizing win

Is it possible for py2exe to generate standalone executables for programs that need the win32com package? I have searched extensively on Google and Stack Overflow but couldn't find a clear answer. ...

Challenges arise when utilizing SQL and Python for extracting data

I'm facing an issue while retrieving values from a database using sqlite3. Every time I extract values, one number is in this format: [(6,)] It appears to be a tuple. However, I only want the value 6 without the ,, (, and [. Any help would be great ...

Designing a border within matplotlib

I have a created a unique plot using matplot lib and want to enhance it with an inset. The data I need for the plot is stored in a dictionary that I use for other visuals as well. Within a loop, I fetch this data and then proceed to run the same loop for c ...

There is no file or directory available, yet all files are contained within the same folder

Encountering a troubling No such file or directory: 'SnP1000_TICKR.csv' error, despite having all my files neatly stored in this specific folder: https://i.stack.imgur.com/mLXXJ.png I am trying to access the file from this location: https://i. ...