How to find this element in Selenium?
Question:
The HTML is: <pre style="word-wrap: break-word; white-space: pre-wrap;">{ "name": "RATE_LIMIT_REACHED", "message": "Too many requests. Blocked due to rate limiting.", "debugId": "f518054e243d2" }</pre>
Can you please help me out with this one?
Answers:
Try using this XPath locator:
"//*[name()='pre' and(contains(@style,'RATE_LIMIT_REACHED'))]"
or
"//*[name()='pre'][contains(@style,'RATE_LIMIT_REACHED')]"
The above XPath expressions are actually the same.
I think we do not need to use name()
here, so this should work as well
"//pre[contains(@style,'RATE_LIMIT_REACHED')]"
UPD
The first issue here resolved by this code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("--start-maximized")
s = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(options=options, service=s)
wait = WebDriverWait(driver, 10)
recaptchaTitle = "//*[contains(text(),'セキュリティチェック')]"
driver.get("https://www.paypal.com/signin?country.x=JP&locale.x=ja_JP")
wait.until(EC.element_to_be_clickable((By.ID, "email"))).send_keys("[email protected]")
try:
wait.until(EC.visibility_of_element_located((By.XPATH, recaptchaTitle)))
print("Recaptcha detected!")
except:
pass
wait.until(EC.element_to_be_clickable((By.ID, "btnNext"))).click()
try:
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "[name='recaptcha']")))
print("Recaptcha detected!!!")
except:
pass
wait.until(EC.element_to_be_clickable((By.ID, "password"))).send_keys("mistery69")
wait.until(EC.element_to_be_clickable((By.ID, "btnLogin"))).click()
try:
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "[name='recaptcha']")))
print("Recaptcha detected!!!!!!!!!")
except:
pass
Well, that depends on where the element is situated in relation to the rest of the document. If it’s the only element with a pre
tag, you can simply use this:
from selenium.webdriver.common.by import By
# ...
elem = driver.find_element(By.TAG_NAME, "pre")
If the page is more complex than that, you can use an XPath such as the one suggested by Prophet.
@duyanhhz you can try the following XPath. Since the "RATE_LIMIT_REACHED" is part of visible text and not part of the style
attribute of pre
HTML tag:
//pre[contains(text(), 'RATE_LIMIT_REACHED')]
The HTML is: <pre style="word-wrap: break-word; white-space: pre-wrap;">{ "name": "RATE_LIMIT_REACHED", "message": "Too many requests. Blocked due to rate limiting.", "debugId": "f518054e243d2" }</pre>
Can you please help me out with this one?
Try using this XPath locator:
"//*[name()='pre' and(contains(@style,'RATE_LIMIT_REACHED'))]"
or
"//*[name()='pre'][contains(@style,'RATE_LIMIT_REACHED')]"
The above XPath expressions are actually the same.
I think we do not need to use name()
here, so this should work as well
"//pre[contains(@style,'RATE_LIMIT_REACHED')]"
UPD
The first issue here resolved by this code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("--start-maximized")
s = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(options=options, service=s)
wait = WebDriverWait(driver, 10)
recaptchaTitle = "//*[contains(text(),'セキュリティチェック')]"
driver.get("https://www.paypal.com/signin?country.x=JP&locale.x=ja_JP")
wait.until(EC.element_to_be_clickable((By.ID, "email"))).send_keys("[email protected]")
try:
wait.until(EC.visibility_of_element_located((By.XPATH, recaptchaTitle)))
print("Recaptcha detected!")
except:
pass
wait.until(EC.element_to_be_clickable((By.ID, "btnNext"))).click()
try:
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "[name='recaptcha']")))
print("Recaptcha detected!!!")
except:
pass
wait.until(EC.element_to_be_clickable((By.ID, "password"))).send_keys("mistery69")
wait.until(EC.element_to_be_clickable((By.ID, "btnLogin"))).click()
try:
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "[name='recaptcha']")))
print("Recaptcha detected!!!!!!!!!")
except:
pass
Well, that depends on where the element is situated in relation to the rest of the document. If it’s the only element with a pre
tag, you can simply use this:
from selenium.webdriver.common.by import By
# ...
elem = driver.find_element(By.TAG_NAME, "pre")
If the page is more complex than that, you can use an XPath such as the one suggested by Prophet.
@duyanhhz you can try the following XPath. Since the "RATE_LIMIT_REACHED" is part of visible text and not part of the style
attribute of pre
HTML tag:
//pre[contains(text(), 'RATE_LIMIT_REACHED')]