0

sub i have a problem i need to find in the html "Derzeit nicht verfügbar."

<div id="availability" class="a-section a-spacing-none }">

<span class="a-size-medium a-color-price">    

Derzeit nicht verfügbar.


<div id="all-offers-display" class="a-section">

I tried it already with below code but doesn't work.

driver.find_elements_by_xpath("//span[@class='a-size-medium a-color-price' and text()='Derzeit nicht verfügbar. ']")

3
  • What's the error when it did not work ? Commented Sep 12, 2021 at 11:57
  • maybe first get all text() and see what you get. This text in HTML has also spaces, tabs, enters which you don't have in text()='Derzeit nicht verfügbar. '. OR you should use something like contains(text(), 'Derzeit nicht verfügbar.') Commented Sep 12, 2021 at 12:56
  • OR maybe you should check 'Derzeit nicht verfügbar.' without space after dot. Commented Sep 12, 2021 at 13:22

1 Answer 1

1

I can get it if I use text without space after dot

text()='Derzeit nicht verfügbar.'

But sometimes it is simpler to check if it contains expected text.

contains(text(), 'Derzeit nicht verfügbar.')

Testing example

from selenium import webdriver

html = '''
<span class="a-size-medium a-color-price">Derzeit nicht verfügbar.</span>
<span class="a-size-medium a-color-price">Derzeit nicht verfügbar. </span>
<span class="a-size-medium a-color-price"> Derzeit nicht verfügbar. </span>

<div id="availability" class="a-section a-spacing-none }">
<span class="a-size-medium a-color-price">

Derzeit nicht verfügbar. 

<div id="all-offers-display" class="a-section">
Other text
'''

driver = webdriver.Firefox()
driver.get("data:text/html;charset=utf-8," + html)

print('\n--- Example 1 - with space ---\n')

all_items = driver.find_elements_by_xpath("//span[@class='a-size-medium a-color-price' and text()='Derzeit nicht verfügbar. ']")
for item in all_items:
    print(item.text)
    print('---')

print('\n--- Example 2 - without space ---\n')

all_items = driver.find_elements_by_xpath("//span[@class='a-size-medium a-color-price' and text()='Derzeit nicht verfügbar.']")
for item in all_items:
    print(item.text)
    print('---')

print('\n--- Example 3 - contains ---\n')

all_items = driver.find_elements_by_xpath("//span[@class='a-size-medium a-color-price' and contains(text(), 'Derzeit nicht verfügbar.')]")
for item in all_items:
    print(item.text)
    print('---')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.