What is the difference here that prevents this from working?

2024/11/16 1:27:14

I'm reading a list of customer names and using each to find an element.

Before reading the list, I make can confirm this works when I hard-code the name,

    datarow = driver.find_element_by_xpath("//span[contains(text(),'ACME Anvil Company')]")

But when I read in the customer list and use it like this, I get a NoSuchElement exception. I know I'm getting the name into the customer variable because the print statement confirms it.

for customer in customerlist:print("START OF DATA FOR CUSTOMER: " +customer)datarow = driver.find_element_by_xpath("//span[contains(text(),'"+customer+"')]")

Do I have something wrong with the '" +customer+ "' part? I've tried it a bunch of different ways.

Answer

Possibly the list elements e.g. customer, includes leading or trailing white spaces. So when you print through print() statement you are overseeing those.

But when you use the xpath as:

datarow = driver.find_element_by_xpath("//span[contains(text(),'"+customer+"')]")

Those whitespaces comes into play and no matches are found.


Solution

You can use the following solution:

datarow = driver.find_element_by_xpath("//span[contains(.,'"+customer+"')]")

Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategy:

datarow = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(.,'"+customer+"')]")))
https://en.xdnf.cn/q/118988.html

Related Q&A

How can I extract numbers based on context of the sentence in python?

I tried using regular expressions but it doesnt do it with any context Examples:: "250 kg Oranges for Sale" "I want to sell 100kg of Onions at 100 per kg"

Chart with secondary y-axis and x-axis as dates

Im trying to create a chart in openpyxl with a secondary y-axis and an DateAxis for the x-values.For this MWE, Ive adapted the secondary axis example with the DateAxis example.from datetime import date…

Env var is defined on docker but returns None

I am working on a docker image I created using firesh/nginx-lua (The Linux distribution is Alpine): FROM firesh/nginx-luaCOPY ./nginx.conf /etc/nginx COPY ./handler.lua /etc/nginx/ COPY ./env_var_echo.…

No Browser is Open issue is coming when running the Robot framework script

I have created Test.py file which has some function in it and using those function names as Keywords in sample.robot file. Ex: - Test.pydef Launch_URL(url):driver.get(url)def article(publication): do…

How to run python file in Tkinter

Im a beginner in Python, hence the question. i would like to run a python file (smileA.py) in Tkinter. How would i start? I do not wish for it to run when clicking a button, but the file to run automa…

Discord.py Cogs “Command [ ] is not found”

I am recoding my discord.py bot using cogs as it wasnt very nice before. I tried this code: import discord import os import keep_alive from discord.ext import commands from discord.ext.commands import …

Binomial distribution CDF using scipy.stats.binom.cdf [duplicate]

This question already has answers here:Alternatives for returning multiple values from a Python function [closed](14 answers)Closed 2 years ago.I wrote below code to use binomial distribution CDF (by u…

How to get Cartesian product of two iterables when one of them is infinite

Lets say I have two iterables, one finite and one infinite:import itertoolsteams = [A, B, C] steps = itertools.count(0, 100)I was wondering if I can avoid the nested for loop and use one of the infinit…

Function to create nested dictionary from lists [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Pygame not using specified font

So I am having a problem in pygame where I specify the font and size to use, but when my program is run, the font and size are the default.Here is where I define the textdef font(self):*****FATAL - THI…