0

/!\ The problem here is a f-string problem; I know how to click the "onclick" element in another ways, but I want to understand why it doesnt work THAT way /!\

I've shorten the code below to make it easier to understand.

driver = webdriver.Chrome(options=options, desired_capabilities=capabilities)

url = f'https://www.cnrtl.fr/definition/coupe'
driver.get(url)

parent_tab = driver.find_element(By.ID, 'vtoolbar')
tabs = parent_tab.find_elements(By.TAG_NAME, 'a')

for i in range(len(tabs)):
    print(driver.find_element(By.XPATH, f"//a[contains(@onclick, 'return sendRequest(5'/definition/coupe//{i}');')]").text)

So, if i try with the complete onclick text

enter image description here

It doesn't work.

If I try to replace:

'return sendRequest(5'/definition/coupe//{i}');'

which is the HTML code for the tabs:

enter image description here

with XPATH below, it works:

'/definition/coupe//{i}'

If i try - just to test the element - with the XPATH below, it works too:

'return sendRequest(5'

So the problem is obviously the ' around /definition/coupe//{i}

I tried the triple ''' and """ following the threads listed below unfortunately without success:

Escaping quotation marks in f string interpolation

Weird Behavior of f-strings and print when used together in Python

How to do Python f strings in triple quotes

And read the point 4 of this article:

https://www.datacamp.com/tutorial/f-string-formatting-in-python

Drakax
  • 657
  • 8

0 Answers0