How to find this element in Selenium? - python

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:\webdrivers\chromedriver.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("jleonhard-bassek#t-online.de")
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')]

Related

Unable to locate element on a webpage using xpath in selenium (python)

I am trying to get some data from a website called : https://dexscreener.com/ethereum/0x1a89ae3ba4f9a97b10bac6a77061f00bb956858b
and i'm trying to get the element : /html/body/div[1]/div/main/div/div[2]/div/div[2]/div/div/div[1]/div[4]/div[2]/div[1]/div[1]/div[2]/span[2] which is basically a number on the webpage representing volume.
i used this code here:
driver.get('https://dexscreener.com/ethereum/' + str(tokenadress))
try:
fivemVolume = WebDriverWait(driver, delay).until(EC.presence_of_element_located(
(By.XPATH, '/html/body/div[1]/div/main/div/div[2]/div/div[2]/div/div/div[1]/div[4]/div[2]/div[1]/div[1]/div[2]/span[2]')))
except:
#more codee
I think its something to do with the webpage loading into some iframe as a default but when i added this code it didn't help:
driver.switch_to.default_content()
Your locator do not match any element on that page.
Elements you trying to access are inside iframe.
So, you need first to switch into the iframe.
The following code should work but I had problems running Selenium on that page since it is blocked by cloudflare:
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")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)
url = "https://dexscreener.com/ethereum/0x1a89ae3ba4f9a97b10bac6a77061f00bb956858b"
driver.get(url)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id*='tradingview']")))
value = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-name='legend-source-item'] [class='valueItem-1WIwNaDF'] .valueValue-1WIwNaDF"))).text
print(value)

Cannot login to Twitter with Selenium

Here is my code
driver_path = Service(r"C:\Users\Lenovo\Desktop\chromedriver.exe")
driver = webdriver.Chrome(service=driver_path)
driver.get('https://twitter.com/login/')
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[#id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[5]/label/div/div[2]/div/input')))
name = driver.find_element(By.CLASS_NAME, 'r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu')
name.send_keys('username')
next = driver.find_element(By.XPATH, '//*[#id="react-root"]/div/div/div/main/div/div/div/div[2]/div[2]/div/div[6]')
next.send_keys(Keys.ENTER)
However, Selenium cannot find the name box regardless of how I try to give different xpaths or classes. I also tried to login from different url's such as
driver.get('https://twitter.com/i/flow/login')
but still couldn't proceed to the next step. What am I doing wrong?
Solution
I see name='text' is unique in HTML DOM. SO there is a way better approach will be to induce ExplicitWait.
Code:
self.browser.maximize_window()
wait = WebDriverWait(self.browser, 30)
self.browser.get('https://www.twitter.com/login')
username_input = wait.until(EC.visibility_of_element_located((By.NAME, "text")))
username_input.send_keys('send username here')
Import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
CSS selector will be:
input[name='text']
XPATH will be:
//input[#name='text']

Selenium not printing inner text of div

I am using selenium to try to scrape data from a website (https://www.mergentarchives.com/), and I am attempting to get the innerText from this element:
<div class="x-paging-info" id="ext-gen200">Displaying reports 1 - 15 of 15</div>
This is my code so far:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
driver.maximize_window()
search_url = 'https://www.mergentarchives.com/search.php'
driver.get(search_url)
assert 'Mergent' in driver.title
company_name_input = '//*[#id="ext-comp-1009"]'
search_button = '//*[#id="ext-gen287"]'
driver.implicitly_wait(10)
driver.find_element_by_xpath(company_name_input).send_keys('3com corp')
driver.find_element_by_xpath(search_button).click()
driver.implicitly_wait(20)
print(driver.find_element_by_css_selector('#ext-gen200').text)
basically I am just filling out a search form, which works, and its taking me to a search results page, where the number of results is listed in a div element. When I attempt to print the text of this element, I simply get a blank space, there is nothing written and no error.
[Finished in 21.1s]
What am I doing wrong?
I think you may need explicit Wait :
wait = WebDriverWait(driver, 10)
info = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#class = 'x-paging-info' and #id='ext-gen200']"))).get_attribute('innerHTML')
print(info)
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You may need to put a condition by verifying if search results loaded or not and once its loaded you can use below code
print(driver.find_element_by_id('ext-gen200').text)

How to select a list in Selenium?

I'm trying to enter an address then they propose me some addresses and I had no idea how to select the first option they give me.
If you want to try, at the second step on this link: https://www.sneakql.com/en-GB/launch/culturekings/womens-air-jordan-1-high-og-court-purple-au/register
adresse = chrome.find_element_by_id('address-autocomplete')
adresse.send_keys(row['Adresse']) #Adress from a file
time.sleep(5)
country = chrome.find_element_by_xpath('//li[#id="suggestion_0"]').click();
Inspect element:
Try clicking on the first option with this:
driver.find_element_by_xpath('//li[#id="suggestion_0"]')
UPD
The element you trying to click is out of the view. You have to do the following:
from selenium.webdriver.common.action_chains import ActionChains
suggestion_0 = driver.find_element_by_xpath('//li[#id="suggestion_0"]')
actions = ActionChains(driver)
actions.move_to_element(suggestion_0).perform()
suggestion_0.click()
You should click this field and wait for the first option to become clickable.
I've wrote some code to test if my solution works and it works in all cases for me:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
url = 'https://www.sneakql.com/en-GB/launch/culturekings/womens-air-jordan-1-high-og-court-purple-au/register'
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get(url)
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'AGREE')]"))).click() # ACCEPT COOKIES
# Making inputs of the first page
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#firstName"))).send_keys("test")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#lastName"))).send_keys("Last name")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#preferredName"))).send_keys("Mr. President")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#email"))).send_keys("mr.president#gmail.com")
driver.find_element_by_css_selector("#password").send_keys("11111111")
driver.find_element_by_css_selector("#phone").send_keys("222334413")
driver.find_element_by_css_selector("#birthdate").send_keys("2000-06-11")
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Next')]"))).click()
# Second page and answer to your main question
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#address-autocomplete"))).send_keys("street")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#suggestion_0"))).click()
Please note, that not all explicit waits are required and I used css selectors because I am not sure that all elements ids are correct.
My output:

Acessing shadow DOM elements with selenium

Basically, I want to acess acess a button that appears in a print preview pop up, which is a shadow dom element. I tried some solutions I found around stackoverflow, but they didn't work, just as the current code I'm trying to build up from, which is:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
def expand_shadow_root(element):
shadow_root = driver.execute_script("return arguments[0].shadowRoot", element)
return shadow_root
root1 = driver.find_element_by_tag_name("print-preview-button-strip")
shadow_root1 = expand_shadow_root(root1)
btn = shadow_root1.find_element_by_xpath("/html/body/print-preview-app//print-preview-sidebar//print-
preview-button-strip//cr-button[1]")
wait.until((EC.element_to_be_clickable(By.XPATH,"/html/body/print-preview-app//print-preview-
sidebar//print-preview-button-strip//cr-button[1]")))
btn.click()
Inspect Element print
try below xpath :
//cr-button[#class='action-button' and #role='button']
In case anyone has the same problem, just insert "-kiosk-printing" as an argument, like this:
options = webdriver.ChromeOptions()
options.add_argument("-kiosk-printing")
driver = webdriver.Chrome(options=options)
Solved the problem for me.

Categories

Resources