I got to the point of grabbing a list of elements by xpath but having trouble grabbing the html behind it.
with this I grab the followers found by the xpath and print them out as how many I received, but I need to get the html of that xpath so that I can recive the text included in them
old_followers = 0
followers = driver.find_elements_by_xpath('//*[#role="dialog"]//ul/div/li')
dialog = driver.find_element_by_xpath('//div[#class="isgrP"]')
while old_followers != followers:
old_followers = followers
driver.execute_script("arguments[0].scrollBy(0,600)", dialog)
time.sleep(2)
driver.execute_script("arguments[0].scrollBy(0,600)", dialog)
time.sleep(2)
followers = driver.find_elements_by_xpath('//*[#role="dialog"]//ul/div/li')
print(len(followers))
Assuming you are using the Selenium Webdriver, when getting the element:
element.get_attribute('innerHTML')
Possibly a similar question: Get HTML Source of WebElement in Selenium WebDriver using Python
Related
I am trying to write in the text box in this html code with python
I tried something like:
frames = driver.find_elements_by_tag_name("iframe")
for f in frames:
print ("FRAMES ID:", f.get_attribute('id'),"Frame names", f.get_attribute('name'))
id_report = f.get_attribute('id')
iframe = driver.find_element_by_id(id_report)
driver.switch_to.frame(iframe)
elem = driver.find_elements_by_xpath(("//*\[#id='b_s2_s11_l2s11_ctl00_reg_period<>_i'\]"))
elem.click()
HTML --> here
But when I do that I get this error message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
this looks like a dynamically generated id, which means it is different in every session. Try to run a non-headless browser with your script and check if the id is different. If so, you should use a different locator.
I could help more, if I had the url or at least the html you want to automate.
This code is extracting links from given URLs. Want to chalk out clickable and non clickable links from the extracted links.
from selenium import webdriver
import time
ccount=0
ncount=0
URL = input('Enter URL: ')
driver = webdriver.Chrome()
URL = input('Enter URL: ')
driver.get(URL)
time.sleep(2)
ids = driver.find_elements_by_xpath('//*[#href]')
for a in ids:
print(a.get_attribute('href'))
There is no specific/ predefined method for clickable link in Selenium, we have to perform click operation and validate any UI elements/ element attributes
Loop over the clickableCount array
1. Save the current web url
2. Click on the link
3. Check whether URL changed or not and continue until end
So i tried to practice on my selenium skills using the website https://instagram.com
I Can Find an object using selenium & click, can not send keys to it.
So basically , i tried to automate comments on Instagram , iv'e found the "Add Comment" , successfully clicked on it , but when i tried to send keys i got an error.
CODE SECTION:
comment_picture = driver.find_elements_by_tag_name('textarea')
for l in comment_picture:
try:
print l.get_attribute("class")
l.click()
time.sleep(1)
l.send_keys('test')
ERROR SECTION:
Message: stale element reference: element is not attached to the page document
The expected result should be that i can comment on each photo on Instagram.
I don't want an answer. i really want to learn selenium. if someone know that i'm doing wrong , it would be great if i will get an hint and not a full answer.
stale element is because the element has been modified when you click, you have to re-find the element like this
comment_picture = driver.find_elements_by_tag_name('textarea')
index = 1 # xpath index start from 1
for txt in comment_picture:
try:
txt.click()
time.sleep(1)
# re-search the textarea
txt = driver.find_element_by_xpath('(//textarea)[%s]' % index)
txt.send_keys('test')
index = index + 1
element references returned by the .find_element_* functions are from the intial page load. When you click(), you are navigating away from the intial page, making all of the element references stale. You will need to call find_elements again before you send keys to the new elements.
You have to make sure you are signed in and able to comment.
also executable_path='your/path'
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Chrome(executable_path="/home/chromedriver")
driver.get('https://www.instagram.com/p/Bs0y4Myg3Hk/')
comment=driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/div/article/div[2]/section[3]/div/form/textarea')
comment.send_keys('hello')
comment.send_keys(Keys.RETURN)
sleep(10)
driver.close()
I have a list of URLs that I need to iterate over. The process I am working on is that selenium opens each of the URLs in the list, clicks a button to open the form, and pass some strings into the form.
I have gotten to the point of clicking the button to open the form. I can not pass in any strings into the form however using any of the elements. I get error 'Unable to locate element'
This is my code so far, easily_apply is list of the URLs:
for i in easily_apply:
driver.get(i)
test = driver.find_element_by_class_name('button')
test.click()
test.find_element_by_name("applicant.name")
test.send_keys("John Smith")
This is the HTML in question:
<input type="text" aria-describedby="label-input-applicant.name-error" aria-labelledby="label-input-applicant.name" id="input-applicant.name" name="applicant.name" class="icl-TextInput-control icl-TextInput-control--sm">
Thank you in advance.
edit:
Code with xpath, not working, getting error 'Unable to locate elements':
for i in easily_apply:
driver.get(i)
test = driver.find_element_by_class_name('indeed-apply-button')
test.click()
test.find_element_by_xpath('//input[#type="text"]')
test.send_keys("John Smith")
edit2:
code with wait in it, still getting same error 'Unable to locate elements':
for i in easily_apply:
driver.get(i)
test = driver.find_element_by_class_name('indeed-apply-button')
test.click()
wait = ui.WebDriverWait(driver,60)
test.find_element_by_name('applicant.name')
test.send_keys("John Smith")
I looked at the html, assuming you are using the code from your previous post, obtaining all the easily apply list.
The element you are looking for is inside nested iframe. you need to switch to that iframe and then look for the element
Replace the time.sleep with Webdriverwait
driver.find_element_by_css_selector('a[class="indeed-apply-button"]').click()
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[name*=indeed-ia]'))
import time
time.sleep(5)
driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
driver.find_element_by_id('input-applicant.name').send_keys('Applicant Name')
Try using xpath.
In Selenium automation, if the elements are not found by the general locators like id, class, name, etc. then XPath is used to find an element on the web page .
Syntax is:
Xpath=//tagname[#attribute='value']
Hope this helps.
driver.find_element_by_css_selector("#jl_1c27f21fec51d296 > a").click()
time.sleep(10)
driver.find_element_by_css_selector('a[class="indeed-apply-button"]').click()
I am very new to web scraping with Python. In the web page, which I am trying to scrape, I can enter string 'ABC' in the text box and click search. This gives me the details of 'ABC', but under the same URL. There is no change in url. I am trying to scrape the result details information.
I have worked till the "search" click. But I do not know how to capture the results of the search (details of search string 'ABC'). Please suggest how could I achieve it.
from selenium import webdriver
import webbrowser
new = 2 # open in a new tab, if possible
path_to_chromedriver = 'C:/Tech-stuffs/chromedriver/chromedriver.exe' # change path as needed
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'https://www.federalreserve.gov/apps/mdrm/data-dictionary'
browser.get(url)
browser.find_element_by_xpath('//*[#id="form0"]/table/tbody/tr[2]/td/label[2]').click()
browser.find_element_by_xpath("//select[#id='SelectedReportForm']/option[#value='1']").click()
browser.find_element_by_xpath('//*[#id="Search"]').click()
Use find_elements_by_xpath() to locate the xpath which entails all of the search results. Then iterate through them using a for loop and print each result's text. That should, at the bare minimum, get what you want.
results = browser.find_elements_by_xpath('//table//tr')
for result in results:
print "%s\n" % result.text