How do I edit CodeMirror with Selenium in Python? - python

I keep getting the error message below every time I try to insert text into CodeMirror on my webpage. Does anyone know how to successfully edit codemirror with selenium?
WebDriverException: Message: unknown error: Cannot read property 'setValue' of undefined
This is my Selenium-Python code
def click_component_script_editor(self):
driver = self.driver
line18Edit = self.driver.find_element(By.XPATH, "//html//div[#class='CodeMirror-line']//div[18]/pre[1]")
driver.execute_script("arguments[0].CodeMirror.setValue(arguments[1]);",
line18Edit,
"foo.bar")

Figured out the answer to this one, I had to use actionChains instead of just regular old send_keys.
codeMirror = self.driver.find_element(".CodeMirror")
action_chains.click(codeMirror).perform()
action_chains.send_keys("Hello World").perform()

Related

Want to click download csv button using Selenium Python, but button changes class-name when hovered over?

I'm trying to save a csv file by clicking the download csv button on a website. However, I noticed the .click() action not doing anything, and I found that the class-name of the button changes from 'export-button is-csv' to 'export-button is-csv hovering.' However, when I try find_element_by_class_name() on the new class name, it returns an error saying it's not there. Here's my code:
driver = webdriver.Chrome('chromedriver',options=options)
driver.get('https://www.rotowire.com/basketball/injury-report.php')
time.sleep(1)
download_csv=driver.find_element_by_class_name('export-button.is-csv')
download_csv.find_element_by_class_name('export-button.is-csv.hovering').click()
Here is the error message I receive:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".export-button is-csv.hovering"}
(Session info: headless chrome=94.0.4606.71)
Was wondering what the specific fix to this is(am using Google Colabs and am new to Selenium).
Just get the table straight from the source then use pandas to convert to dataframe and write to disk:
import requests
import pandas as pd
url = 'https://www.rotowire.com/basketball/tables/injury-report.php?team=ALL&pos=ALL'
jsonData = requests.get(url).json()
df = pd.DataFrame(jsonData)
df.to_csv('file.csv', index=False)
You can directly use
button.is-csv
css selector to click on it.
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.rotowire.com/basketball/injury-report.php')
time.sleep(1)
download_csv = driver.find_element_by_css_selector('button.is-csv')
download_csv.click()

scraping not working - InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified

I am trying to scrape the yahoo finance webpage (comment section). I want to click on a button whose class is seen in the picture below:
I want to select the button with the following code, but I am getting an InvalidSelectorException. I do not understand why.
Note that in my code I have replaced the space with . because that's what I usually do, but I have also tried without replace the spaces and in both cases it is not working.
link = 'https://finance.yahoo.com/quote/AMD/community?p=AMD'
path = r"""chromedriver.exe"""
driver = webdriver.Chrome(executable_path=path)
driver.get(link)
driver.find_element_by_class_name('sort-filter-button.O(n):h.O(n):a.Fw(b).M(0).P(0).Ff(i).C(#000).Fz(16px)')
You can check the below
#This page is taking more time to load
sleep(15)
element = driver.find_element_by_xpath("//button[#aria-label='Sort Reactions']")
element.click()
update
element = driver.find_element_by_xpath("//button[contains(#class,'sort-filter-button')]")
element.click()

How to fix NoSuchElementException on webdriver

I have tried to log in a portal of Wifi automatically using python. However, find_element_by_X gives errors.
I am using Chrome as a browser.
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://hinet.hiroshima-u.ac.jp/loginweb.html')
time.sleep(2)
#driver.find_element_by_css_selector('a.button').click()
username = driver.find_element_by_css_selector("input")
username.clear
#Enter HiroshimaU ID
username.send_keys('input_username')
password = driver.find_element_by_name('pwd')
password.clear
password.send_keys('input_userpassword')
This code should work, but it just gives me errors:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"input"}
I have tried other methods, such as by_name or by_id. But none of them is working.
I am a very beginner so my question might not be clear, but I appreciate your help.
Edit(Oct 23, 2019):
I am sorry you cannot access the portal site.
I hope this screenshot may help.
Portal site
Input is not a valid cssSeletor.
You need top do something like below:
Example:
for https://www.google.com/ website,
cssSeletor of textFiled would be
input[name='q']
First, I couldn't get access to the site you mentioned, as it helps to see how the html formed.
Second based on that, I ask you to have a look at python selenium find_element_by_name
It shows how to access element by name as:
elem=browser.find_element_by_name("Email")
where you can see in the html the name tag of "Email"

Log in to website using python and selenium

I'm trying to log in to http://sports.williamhill.com/bet/en-gb using python and selenium.
Here is what I've tried so far:
from selenium import webdriver
session = webdriver.Chrome()
session.get('https://sports.williamhill.com/bet/en-gb')
# REMOVE POP-UP
timezone_popup_ok_button = session.find_element_by_xpath('//a[#id="yesBtn"]')
timezone_popup_ok_button.click()
# FILL OUT FORMS
usr_field = session.find_element_by_xpath('//input[#value="Username"]')
usr_field.clear()
WebDriverWait(session, 10).until(EC.visibility_of(usr_field))
usr_field.send_keys('myUsername')
pwd_field = session.find_element_by_xpath('//input[#value="Password"]')
pwd_field.clear()
pwd_field.send_keys('myPassword')
login_button = session.find_element_by_xpath('//input[#id="signInBtn"]')
login_button.click()
I'm getting the following error.
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
when trying to execute
usr_field.send_keys('myUsername')
The usr_field element seems to be visible if I'm viewing it with the inspector tool, however I'm not 100% sure here.
I'm using this script (with some modifications) successfully on other sites, but this one is giving me a real headache and I can't seem to find the answer anywhere on the net.
Would appreciate if someone could help me out here!
The following code will resolve the issue.
from selenium import webdriver
session = webdriver.Chrome()
session.get('https://sports.williamhill.com/bet/en-gb')
# REMOVE POP-UP
timezone_popup_ok_button = session.find_element_by_xpath('//a[#id="yesBtn"]')
timezone_popup_ok_button.click()
# FILL OUT FORMS
user_element = session.find_element_by_name("tmp_username")
user_element.click()
actual_user_elm = session.find_element_by_name("username")
actual_user_elm.send_keys("myUsername")
password_element = session.find_element_by_id("tmp_password")
password_element.click()
actual_pass_element = session.find_element_by_name("password")
actual_pass_element.send_keys("myPassword")
login_button = session.find_element_by_xpath('//input[#id="signInBtn"]')
login_button.click()

How to put a text into the textfield using selenium?

i'm using selenium 2.46.0 and python 3.4
I want to put the string "Hello, world!" into the textfield for creating new letter.
Am i doing this the right way? I try to switch the frame and find the right element to put the text into it.
My code:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://mail.ru/')
browser.find_element_by_name('Login').clear()
browser.find_element_by_name('Login').send_keys('selenium90')
browser.find_element_by_name('Password').clear()
browser.find_element_by_name('Password').send_keys('pythonrocks')
browser.find_element_by_id('mailbox__auth__button').click()
browser.find_element_by_css_selector('a.b-toolbar__btn').click()
browser.find_element_by_css_selector('textarea.js-input:nth-child(5)').send_keys('07ufo#mail.ru')
browser.switch_to_frame(browser.find_element_by_id('compose_462_composeEditor_ifr'))
browser.find_element_by_class_name('mceContentBody').send_keys('Hello, world!')
selenium.common.exceptions.NoSuchElement
Exception: Message: Unable to locate element: {"method":"id","selector":"compose_462_composeEditor_ifr"}
I tried:
iframe = browser.find_elements_by_tag_name('iframe')[frameindex]
browser.switch_to_frame(iframe)
browser.find_element_by_css_selector('body.tinymce').send_keys('hoo')
But it did not help
i added a screenshot of tree:
The id of that element is dynamically generated! Try something like:
browser.switch_to_frame(browser.find_element_by_xpath('//frame[contains(#id, "composeEditor_ifr")]'))

Categories