How do I get the element Datetime using selenium without using XPath? - python

how do i find that class datetime element using selenium.
not xpath please as that dont work on headless chrome and i do not want to see the browser showing up when i am coding this to run in background every hr.
i've tried this
pp = p.find_element_by_class_name('line-height-3')
print(pp.get_attribute('datetime'))
results
none
i've figured it out
Announcement = driver.find_element_by_class_name('discussion.subscribed')
if Announcement.is_displayed():
Announcement_id = Announcement.get_attribute('data-discussionid')
Announcement_title = Announcement.find_element_by_class_name('w-100.h-100.d-block').get_attribute('title')
TIME = Announcement.find_element_by_id('time-created-'+str(Announcement_id))
Time = TIME.get_attribute('datetime')
so i just putting it out incase anyone else came by such problem.
The reason why I am not using xpath is that whenever it runs in headless mode
i get nosucherror but in normal mode. it works fine.

Your id is dynamic so you can not use it. You can use cssSelector if xpath is not nice for you.
See more on https://www.browserstack.com/guide/css-selectors-in-selenium
I do not have more HTML of your page, but you can try with:
time[datatimeformat='part that is hard to see on your picture']

Try getting it by id.
pp = p.find_element_by_id('time-created-82672')
print(pp.get_attribute('datetime'))

You can use CSS selector
pp = p.find_element_by_css('div.author-info time')
print(pp.get_attribute('datetime'))
If you will change your mind there xpath :)
.//div[contains(#class, 'author-info')]//time

Related

How can I find the text between a span element with no class/id using Selenium in Python?

I am trying to use Selenium to find the text between a span element that has no class, id, or anything I can think of to find it specifically.
Here is the html:
HTML from inspect element on Chrome
I have tried the following:
reqStr = driver.find_elements_by_xpath('span[data-bind*=text]')
for i in reqStr:
print(i.get_attribute('text'))
But that results in it printing just an empty list '[]'.
Any advice on how to find the text "CHEM1040, PHYS1130 - Must be completed prior to taking this course" is appreciated
You could get it using a CSS Selector like:
reqStr = driver.find_element_by_css_selector('span[data-bind="text: $data.DisplayText() + \' \' + $data.DisplayTextExtension()"]')
Thanks for the help guys. I actually managed to find it using:
reqStr = driver.find_elements_by_xpath("//span[contains(text(),'*')]")
print(reqStr)
for i in reqStr:
print(i.text)
I had this before but must have slightly messed something up because I was not getting the correct results. Thanks!

Unable to switch to iFrame using Selenium

I'm currently trying out Selenium for Python to do some web automation.
I got stuck a the checkout where you have to enter your credit card data.
I'm aware that I have to switch to iFrame to locate the input box.
But I was never able to switch the frame.
I tried it for ID or for example for this:
time.sleep(4)
frame = self.driver.find_element_by_xpath("//iframe[#name= '__privateHeidelpayFrame--heidelpay-holder-iframe-1616489954834']")
self.driver.switch_to.frame(frame)
name_input = self.driver.find_element_by_id("card-holder")
name_input.clear()
name_input.send_keys(credit_card.NAME)
I'm always getting "Unable to locate element : {"method":"xpath","selector":"//iframe[#name= '__privateHeidelpayFrame--heidelpay-holder-iframe-1616489954834']"}".
I added a pic from the Source Code.
Source Code from the Webpage
I'm thankful for any advise!
Best, Jannic
Possibly id & name attribute is dynamic.
Try with starts-with() or use other attribute like class to identify the iframe.
frame = self.driver.find_element_by_xpath("//iframe[starts-with(#name, '__privateHeidelpayFrame--heidelpay-holder-iframe-')]")
self.driver.switch_to.frame(frame)
OR
frame = self.driver.find_element_by_xpath("//iframe[#class='heidelpayUIIframe']")
self.driver.switch_to.frame(frame)
Have you tried using a simpler API to find the iFrame?
Does
self.driver.find_element(By.NAME, "__privateHeidelpayFrame--heidelpay-holder-iframe-1616489954834'")
Return anything?

How to get Id with Selenium Chromedriver with Python?

I want to get the Id of an iFrame directly with Selenium Chromedriver in Python without Javascript if possible, but I don't know why is failing.
When I do this I get wrong Id result as shown below.
>>> driver.find_element_by_class_name("MyClass").id
u'5b6a-c153-4e7f-90b2-b7e45'
If I send the following Javascript command on Chrome Console I get the correct frame Id:
> document.getElementsByClassName('MyClass')[0].id
< "MyFrame89-0-bed65f30"
Now when I try to use the same Javascript command within driver.execute_script() it doesn't show anything.
>>> driver.execute_script("document.getElementsByClassName('MyClass')[0].id")
>>>
So, here I have 2 issues:
1- driver.find_element_by_class_name().id is not showing the correct Id
2- driver.execute_script() it doesn't show anything.
What I'm doing wrong?
Thanks for any help.
UPDATE
A sample html code is here https://jsfiddle.net/k3x9rsa6/1/
The id you're retrieving is NOT the html id attribute.
Try retrieving it one of these ways:
driver.find_element_by_class_name("MyClass").get_attribute("id")
Or
driver.find_element_by_class_name("MyClass").get_property("id")

Selenium Python find element partial link text

I'm trying to use Pythons Selenium module to click on an element whose link has the text "xlsx" at the end of it. Below is the code I'm using and the details of the element. Can someone please see why Python is unable to find this element?
driver.find_element_by_partial_link_text('xlsx').click()
Here is the element details:
<a name="URL$2" id="URL$2" ptlinktgt="pt_new" tabindex="43" onfocus="doFocus_win0(this,false,true);" href="http:******/HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx" onclick="window.open('http:********/HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx','','');cancelBubble(event);return false;" class="PSHYPERLINK">HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx</a>
I had to remove some parts of the URL for confidentiality purposes, however, it should not impact the answering of the question.
Thanks.
Thanks for the replies. Turns out, as #Andersson mentioned, the window was in a different frame.
I solved the problem using the following code before the find_element: driver.switch_to.frame('ptModFrame_0').
You can use a CSS selector:
driver.find_element_by_css_selector("a[href*='xlsx']")
If the element still cannot be located, I would suggest using a wait statement, to ensure that the element is visible, before you interact with it.
Please try:
driver.find_element_by_xpath(".//a[contains(#href,'xlsx')]").
You can grab it by class name (class name = PSHYPERLINK).
This should work:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
url = ''
driver = webdriver.Chrome('/path/to/chromedriver/executable')
if __name__=='__main__':
driver.get(url)
time.sleep(3)
driver.find_element_by_class_name('PSHYPERLINK').click()
When finding the attribute, make sure to use a singular '"element". Like:
driver.find_element_by_class_name('PSHYPERLINK').click()
not:
driver.find_elements_by_class_name('PSHYPERLINK').click()
Hope this helps.

Extracting hidden element in Selenium

I have an element of type hidden in an iframe. I am wondering if there would be any way to get this value as I am using selenium. More specifically it is a captcha field. I've tried pulling it with something along the lines of
#!/usr/bin/env python
from selenium import webdriver
driver=webdriver.Chrome(chrome_bin_path)
driver.get('http://websitehere.com')
print driver.find_element_by_xpath('//*[#id="recaptcha-token"]').text
but because of it's hidden nature it returns nothing.
Below is a snippet of the source.
Highlighted is the string of interest. (value)
driver.switch_to_frame('undefined')
token_value = driver.find_element_by_id('recaptcha-token').get_attribute('value')
driver.switch_to_default_content()
Moving between windows and frames.
Use this method
hidden_text = element.get_attribute("textContent")

Categories

Resources