0

Have been playing around with Selenium Webdriver and i would like to extract certain information from a webpage. Below is the source code for webdriver

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "/usr/local/bin/chromedriver")
driver = webdriver.Chrome ("/usr/local/bin/chromedriver")

browser = webdriver.Chrome(executable_path = DRIVER_BIN)
browser.get('https://www.abuseipdb.com/check/95.47.155.87')

I would like to extract the value 91% from this particular website and save it into a variable. Will need a solutions that are able to extract the % value out even if it's not 91%. Below are the html code.

<p>This IP was reported <b>222</b> times. Confidence of Abuse is <b>91%</b>: <a href="/faq.html#confidence" style="float: right; font-weight: bold;" class=text-muted>?</a></p>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
twistofytd
  • 29
  • 2
  • 6

3 Answers3

0

This will pull the first text of an element with a <b> tag containing % using Xpath:

abusePercentage = browser.find_element_by_xpath('//b[contains(text(), "%")]').text
PixelEinstein
  • 1,713
  • 1
  • 8
  • 17
0

You can use the xpath selector mentioned by @PixelEinstein or use the following css selector to get the text value

text_of_element = browser.find_element_by_css_selector('div.well > p:nth-child(2) >b:nth-child(2)').text
print(text_of_element)

This prints the value 91% of the <p> tag.

demouser123
  • 4,108
  • 9
  • 50
  • 82
0

To extract the texts e.g. 91% you can use the following solution:

driver.find_element_by_xpath("//div[@class='well']/h3//following::p[1]//following::b[2]").get_attribute("innerHTML")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352