Home > Net >  How do I get a word through Selenium?
How do I get a word through Selenium?

Time:12-29

I'd like to extract and use the red alphabet of the code below through 'Selenium', so please give me some advice on how to do it

The alphabet changes randomly on every try


<td>
  <input type="text" name="WKey" id="As_wkey" value="" maxlength="10"  style="width: 300px;" title="password" />
  <span id="myspam" style="padding: 2px;">
    <span style="font-size: 12pt; font-weight: bold; color: red;">H</span>123 
    <span style="font-size: 12pt; font-weight: bold; color: red;">R</span>
    <span style="font-size: 12pt; font-weight: bold; color: red;">8</span>6789 
  </span> 
  (type red word.)
</td>

here is my code

red_characters_element = driver.find_element(By.ID, 'myspam')

red_characters_elements = red_characters_element.find_elements(by = By.CSS_SELECTOR, value="span[style='font-size: 12pt; font-weight: bold; color: red;']")

print(red_characters_elements)

result []

CodePudding user response:

Given all the Red colored alphabets are inside of the <span> tag. You can retrieve it using tag.

red_characters_element = driver.find_element(By.ID, 'myspam')
red_characters_elements = red_characters_element.find_elements(By.TAG_NAME, 'span')
for red_character in red_characters_elements:
    print(red_character.text)

Results :

H
R
8

CodePudding user response:

If you need only red letters, you can try using the java script inside the selenium:

driver.execute_script('return document.querySelectorAll("[style*=red]")')

You get an array of objects where the style has the attribute "red", with the for loop you can get the values or anything else

  • Related