Python Selenium: Variable encoding gets altered by XPath

I am facing an issue with my code that uses XPath to search through text. The problem arises when the text being searched contains special Latin characters like ñ or í.

After encoding the text, it displays correctly when printed. However, the issue occurs when using XPath as the encoding changes and causes the text to not be found.

Here is a snippet of the decoded code:

name_act = "HOTEL DIEGO DE ALMAGRO SAN PEDRO DE ATACAMA"
name_act = name_act.decode("utf8")

contract_name = "Campaña Cyber Day, Desayuno Incluído"
contract_name = contract_name.decode("utf8")
print contract_name

xpath_query = "//select[@name='"+name_act+"']/option[text()='"+contract_name+"']"
print xpath_query
hotel_selection = driver.find_element_by_xpath(xpath_query).click()

Answer №1

Your coding attempts were almost flawless. However, there may not be a need to go through encoding/decoding unless you specifically want to display the characters in the console like this:

current_act_name = "HOTEL DIEGO DE ALMAGRO SAN PEDRO DE ATACAMA"
#act_name = current_act_name.encode("utf-8")

current_contract_name = "Campaña Cyber Day, Desayuno Incluído"
contract_name = current_contract_name.encode("utf-8") #necessary if display in console is required
print contract_name

xpath = "//select[@name='"+current_act_name+"']/option[text()='"+current_contract_name+"']"
selected_hotel = driver.find_element_by_xpath(xpath).click()

Another concern is the incompatibility between the versions of binaries being used:

  • You have chromedriver=2.41
  • The Release Notes for chromedriver=2.41 clearly state the following :

Supports Chrome v67-69

  • You are using chrome=70.0
  • The Release Notes for ChromeDriver v2.44 mention the following :

Supports Chrome v69-71

This indicates a mismatch between ChromeDriver v2.41 and Chrome Browser v70.0

Solution

  • Upgrade ChromeDriver to the latest version ChromeDriver v2.44.
  • Maintain Chrome version within the range of Chrome v69-71 levels. (as per ChromeDriver v2.44 release notes)
  • Clean up your Project Workspace using your IDE and Rebuild your project with necessary dependencies only.
  • If your base Web Client version is outdated, uninstall it using Revo Uninstaller and install a newer GA and released version of Web Client.
  • Run your @Test.

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

The removal of all cookies using `driver.manage().deleteAllCookies()` with Selenium does not function properly in conjunction with browser options

There seems to be an issue with the browser Options in Selenium where driver.manage().deleteAllCookies() is not working as expected. Here is a snippet of the code showcasing this problem. var profile = new firefox.Profile('./fProfile'); profile. ...

Issues with Retrieving Data from Microsoft Dynamics 365 CRM

As of late, I've been given the task of automating MS CRM 365 using Selenium Automation. I've decided to utilize Gradle and Java for this project within IntelliJ. However, a major hurdle I'm facing is the inability to access elements on a f ...

I'm having difficulty changing to an IFrame on Internet Explorer

I recently created a keyword-driven framework which includes an action keyword to switch frames. While it works perfectly fine with Mozilla, I encountered an issue with Internet Explorer where the frame is not switching and it logs an error instead. IE Dri ...

Does AngularJS have a feature similar to jQuery.active?

As I utilize selenium to conduct tests on my application, I am encountering numerous ajax calls that utilize $resource or $http. It would be convenient if there was a method in angular to monitor active ajax requests so that selenium could wait until they ...

Error in Protractor/Selenium Webdriver: The runtime execution context has an invalid 'context'

While testing my Angular2 app using Protractor, everything was functioning properly without any changes in the configuration. However, I now encounter the following error: ... super(opt_error); ^ SessionNotCreatedError: session not created exception ...

What is the process for configuring simultaneous services on CircleCI for testing purposes?

My current project involves running tests with Jasmine and WebdriverIO, which I want to automate using CircleCI. As someone new to testing, I'm a bit unsure of the process. Here's what I've gathered so far: To run the tests, I use npm tes ...

Can the Java driver incorporate PhantomJS's onResourceRequested callback functionality?

Is there a way I can achieve the same thing, perhaps via the Java API? My goal is to prevent css files from loading in PhantomJS using the Java driver. I came across this helpful js code snippet for PhantomJS: page.onResourceRequested = function(requestD ...

Is Selenium Service not recognizing the executable_path parameter?

Currently using Selenium 4.1.5, I encountered an issue while trying to execute the following code: from selenium import webdriver from selenium.webdriver.chrome.service import Service service = Service(executable_path=f'./chromedriver.exe') driv ...

Performing a Href link click using Selenium in PythonIn this tutorial,

What is the best way to interact with this element using Selenium in Python? <a onclick="ClearTrackingCode();" id="ctl00_phMainContent_CampaignGrid_grid_ctl02_btnSelect" title="Edit the selected campaigns" href="javascript:__doPostBack('ctl00$phMa ...

E2E tests for Internet Explorer using Selenium and Protractor

Looking to integrate e2e tests into our CI build process, I have successfully added them for Chrome and Firefox. However, I want to include tests for various versions of Internet Explorer as well. How can this be accomplished in the build process on Linux/ ...

Unable to clear the field as the clear() method is not functioning properly

I am unable to empty the field ('text1) in order to input a new text ('text2') self.driver.find_element_by_id('serviceName').click() self.driver.find_element_by_id('serviceName').clear() self.driver.find_element_by_id(&a ...

Having trouble with clicking the button on the user login page of the rediff.com website

Having trouble clicking on a button after entering the email address. driver = new ChromeDriver(); driver.get("http://in.rediff.com/"); driver.findElement(By.xpath(".//*[@id='homewrapper']/div[5]/a[3]/div/u")).click(); dr ...

I am in search of understanding the criteria for determining when an ISearchContext can be considered

I've been scouring different sources for information on this particular topic, and while I did find a similar answer here, I'm looking for a bit more detail. Here's my code: var element = myWebDriver.FindElement("User_Login"); var finalele ...

Avoid using a dollar sign in a Selenium xpath locator

I need help finding an element that has the attribute $9a, but I am encountering issues because of the dollar sign. Here is the XPath expression I tried to use: //td[@id='isc_6T']//span[@$9a='browse'] However, I received an exception ...

Is there a way to retrieve column values from an Excel spreadsheet by referencing only the column name?

Is there a way to read usernames from the first column and passwords from any column in an Excel sheet without specifying the exact column address? FileInputStream fs = new FileInputStream(strReadFile); Workbook wb = Workbook.getWorkbook(fs) ...

Tips for retrieving Latency and Rendering time with Webdriver sampler in Jmeter

Can anyone assist me with extracting the latency and rendering time values of a web application using Webdriver sampler in jmeter? If possible, please provide some sample webdriver sampler codes to retrieve these values. Anticipated outcomes: 1) Obtain ...

Selenium Python: Navigating to Links with Hidden Text

I am attempting to click on all the links within a web page after selecting certain dates (), but unfortunately, the links do not have unique class names and only contain the tag name "a". There are multiple other elements with the same tag name which make ...

What is the best way to input numerical data into an input field with Python utilizing Selenium?

I am encountering an issue with my script that is writing values into a web page. All values are successfully written except for one field, which consistently displays the error message: https://i.stack.imgur.com/qzx65.png (A screenshot has been included b ...

Using Python to extract all the links on a dynamic webpage

I am struggling to develop a universal crawler that can analyze a webpage and compile a list of all links within it, with the goal of examining an entire domain and all its internal links. I have attempted using HtmlUnit in Java and Selenium in Python, bu ...

Utilizing the same browser session across various scenarios

In the following code, I am attempting to utilize the same browser session in different test cases. However, upon execution, it is observed that two separate browser sessions are being opened for each test case. Any suggestions on how to resolve this iss ...