Trying to find a id in elements in python - python

I need help,
I'm trying to print the link from a website,
Here is how the code looks like:
<div class="tabla_szoveg">
<div style="cursor:pointer" onclick="konyvjelzo('1523442');" class="torrent_konyvjelzo2"></div>
I'm trying to print the number inside "konyvjelzo('1523442');"
Using selenium
Also tried:
linkgettr= driver.find_element_by_class("box_torrent_all")
but getting NONE
Thanks

To print the partial value of onclick event i.e. 1523442 you have to induce WebDriverWait for the desired visibility_of_element_located() and you can use the following solution:
Using CSS_SELECTOR:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.tabla_szoveg>div.torrent_konyvjelzo2"))).get_attribute("onclick").split("'")[1])
Using XPATH:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[class='tabla_szoveg']/div[#class='torrent_konyvjelzo2']"))).get_attribute("onclick").split("'")[1])

Please check if torrent_konyvjelzo2 is dynamic ,if not then you can also replace below torrent text with it otherwise you can use below code as it is.
split("'")[1] is used to split your konyvjelzo('1523442'); text
konyvjelzo( --> item 0
1523442 --> item 1
); --> item 2
the first item index starts from 0. so we can return 1 item.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//div[contains(#class, 'torrent')]")))
attribute=element.get_attribute("onclick")
print attribute.split("'")[1]

i think you could just use simple regex to find this easily
ids = re.findall("onclick=\"konyvjelzo\('(.*?)'\);",page_text)

Related

text based HTML of the element selenium python

I am actually trying to scrap a website. In fact I have a table like this below :
Table
I would like to navigate to the line that contains the word "Maître", but I am not able to find this line with selenium. Below the html code of the page :
Html code 1
Html code 2
And this is my code :
objets_de_risque = driver.find_element(By.ID,'sidebar-link-0-1')
driver.execute_script("arguments[0].click();", objets_de_risque)
code_h = driver.find_element(By.ID,'input-search-1')
code_h.send_keys(Keys.CONTROL + "a")
code_h.send_keys(Keys.DELETE)
code_h.send_keys("H1404")
code_h.send_keys(Keys.ENTER)
driver.switch_to_frame("main1")
line_maitre = driver.find_elements_by_xpath("//*[contains(text(), 'Maître')]")
driver.execute_script("arguments[0].click();", line_maitre)
The last 2 lines doesn't seem to work. Is there a way to go to this line with selenium ?
Thank you very much
find_elements() returns a list. Instead you need find_element() to locate the element.
Solution
To locate a visible element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using XPATH:
line_maitre = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[text()=''Maître'']")))
However to click on it as per your code in a single line:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[text()=''Maître'']"))))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to identify the iframe using Selenium?

HTML:
<iframe allowpaymentrequest="true" allowtransparency="true" src="https://shopify.wintopay.com/
cd_frame_id_="ca9e4ad6a1559de159faff5c1f563d59"
name="WinCCPay"
id="win-cc-pay-frame"
I'm trying to input text in a CC field. Apparently its in an iframe I picked the last one in the HTML and tried to select it from the identifiers above but I keep getting the element couldn't be found
iframe= wd.find_element_by_id("win-cc-pay-frame")
wd.switch_to.frame(iframe)
The frame is currently being shown in the browser so no need for implicit wait.
To identify the <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#win-cc-pay-frame[name='WinCCPay'][src^='https://shopify.wintopay.com']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='win-cc-pay-frame' and #name='WinCCPay'][starts-with(#src, 'https://shopify.wintopay.com')]")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
The problem can be that the name and id of the element are dynamic and change for each unique checkout window? Can you check if adding class attribute at iframe tag and find element by this attribute?
It must be similar to:
iframe = wd.find_element_by_class_name('card-pay-iframe')
wd.switch_to.frame(iframe)
...
wd.switch_to.default_content()
good coding! ¯_(ツ)_/¯

How to write a value for input tag created by ion-input using selenium Python

I have ion-input tags in my app and that creates another input tag as a sibling. The sibling input is responsible for any input value.
I want to access that sibling to enter a value using selenium Python (can be using send_keys or using javascript_executor
<ion-input data-cy="email" type="email" debounce="50" value="" class="sc-ion-input-md-h sc-ion-input-md-s md">
<input class="native-input sc-ion-input-md" autocapitalize="off" autocomplete="off"
autocorrect="off" name="ion-input-0" placeholder="" spellcheck="false" type="email"></ion-input>
Everytime I use that data-cy="email" I am getting elementNotFound exception only.
I am using Selenium python.
The element should be unique in nature, based on the HTML that you've shared, can you check this CSS or XPATH
XPATH:
//input[#class='native-input sc-ion-input-md' and #type='email' and#name='ion-input-0']
CSS_SELECTOR:
input[class='native-input sc-ion-input-md'][type='email'][name='ion-input-0']
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath/css and see, if your desired element is getting highlighted with 1/1 matching node.
If they happen to be unique in nature you can try with the below code:
Code1:
wait = WebDriverWait(driver, 30)
input = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[#class='native-input sc-ion-input-md' and #type='email' and#name='ion-input-0']")))
input.send_keys('the string that you want to send')
Code2:
wait = WebDriverWait(driver, 30)
input = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[#class='native-input sc-ion-input-md' and #type='email' and#name='ion-input-0']")))
driver.execute_script("arguments[0].setAttribute('value', 'the string that you want to send')", input)
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I don't any such major issues in your code attempts. However the the desired element is a dynamic element, so ideally to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ion-input[data-cy='email'] > input[type='email'][name^='ion-input']"))).send_keys("Sarath#N.com")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ion-input[#data-cy='email']/input[#type='email' and starts-with(#name, 'ion-input')]"))).send_keys("Sarath#N.com")
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
It was so easy. I have been using either XPATH or CSS_SELECTOR hence always it does not penetrate to the input tag that was created by ion. Instead of the bespoke one, i have used TAGNAME and that did the magic
email = [(By.XPATH, "ion-input[data-cy='email'] input")]
Instead use like this
email = [(By.TAGNAME, "ion-input[data-cy='email']")]

How to locate the element using XPath Selenium and Python

I need to get the number "3" from this HTML with python selenium
<div class="number">3</div>
This is the XPATH:
//*[#id="roulette-recent"]/div/div[1]/div[1]/div/div
I tried something like
number = navegador.find_element_by_xpath('//*[#id="rouletterecent"]/div/div[1]/div[1]/div/div').get_attribute('class')
If this xpath
//*[#id="rouletterecent"]/div/div[1]/div[1]/div/div
represent the node:
<div class="number">3</div>
and you want to extract the text from it, you should use either:
number = navegador.find_element_by_xpath('//*[#id="rouletterecent"]/div/div[1]/div[1]/div/div').get_attribute('innerText')
print(number)
or
number = navegador.find_element_by_xpath('//*[#id="rouletterecent"]/div/div[1]/div[1]/div/div').text
print(number)
I think you're looking for:
number = navegador.find_element_by_xpath('//*[#id="rouletterecent"]/div/div[1]/div[1]/div/div').text
To print the text 3 you can use either of the following Locator Strategies:
Using css_selector and get_attribute("innerHTML"):
print(navegador.find_element(By.CSS_SELECTOR, "#roulette-recent div.number").get_attribute("innerHTML"))
Using xpath and text attribute:
print(navegador.find_element(By.XPATH, "//*[#id="roulette-recent"]//div[#class='number' and text()]").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and text attribute:
print(WebDriverWait(navegador, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#roulette-recent div.number"))).text)
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(navegador, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[#id="roulette-recent"]//div[#class='number' and text()]"))).get_attribute("innerHTML"))
Note : You have to add the following 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 can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

Python + Selenium: I can't get print text from div

Python + Selenium: I can't get print text from this div:
<div id="modal-content-18" class="modal-content" data-role="content">
<div>
SignUp Failed. Please Try Again.
</div>
</div>
I tried this:
resp = browser.find_element_by_class_name("modal-content").text
print resp
But it does not work.
Please help me.
I personally prefer xpaths because of cases like these. They can tackle many complex cases as well. Try the following:
resp = browser.find_element_by_xpath('//div[#class="modal-content"]/div').text
print resp
In case the element isn't visible on the screen. The text method will be none. In that case you need the textContent attribute. Use the following then:
resp = browser.find_element_by_xpath('//div[#class="modal-content"]/div').get_attribute("textContent")
print resp
Let me know if it works for you. Also make sure there is only one modal-content on the page. In case there are more than one, your css_selector is insufficient to identify this element. To check this you can run the following.
l = len(browser.find_elements_by_xpath('//div[#class="modal-content"]/div'))
print l
if it returns a number greater than 1, then the modal-content class alone isn't enough and you will need to expand on your selection criteria.
Induce WebDriverWait and visibility_of_element_located() and following locator strategy.
Using CLASS_NAME:
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,"modal-content"))).text)
Using XPATH:
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='modal-content' and #data-role='content']"))).text)
You need to import followings.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
EDITED
Check the textContent attribute value.
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,"modal-content"))).get_attribute("textContent"))
OR
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='modal-content' and #data-role='content']"))).get_attribute("textContent"))
The desired text SignUp Failed. Please Try Again. is within the child <div> so you have to induce WebDriverWait for the desired visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.modal-content[id^='modal-content-'][data-role='content']>div"))).get_attribute("innerHTML"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='modal-content' and starts-with(#id, 'modal-content-')][#data-role='content']/div"))).text)
Note : You have to add the following 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 can find a relevant discussion in How to retrieve the title attribute through Selenium using Python?
Outro
As per the documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

Categories

Resources