I try of select the sport 'Football' in a drop down of sport but impossible of click on it.
I tried with the Select() method:
driver = webdriver.Chrome()
url = "https://www.flashscore.com/"
driver.get(url)
Team = 'Paris SG'
Type = 'Teams'
sport = 'Football'
buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__button--search"))).click()
fill_search_bar = driver.find_element(By.CSS_SELECTOR, ".input___1NGxU3-")
fill_search_bar.clear()
fill_search_bar.send_keys(Team)
driver.find_element(By.CSS_SELECTOR, ".dropDown").click()
select_sport = Select(driver.find_element(By.XPATH,"//div[contains(#class, 'dropDown__list')]"))
select_sport.select_by_visible_text(sport)
This code return this error : UnexpectedTagNameException: Message: Select only works on <select> elements, not on <div>.
Here is my second version:
fill_search_bar = driver.find_element(By.CSS_SELECTOR, ".input___1NGxU3-")
fill_search_bar.clear()
fill_search_bar.send_keys(Team)
driver.find_element(By.CSS_SELECTOR, ".dropDown").click()
select_sport = WebDriverWait(driver, timeout=10).until(EC.element_to_be_clickable((By.XPATH,"//[#class='dropDown__list']/[contains(text(),'"+ sport +"')]"))).click()
This code return this error : selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='dropDown__list']/div[contains(text(),'Football')]"}.
How can I solve this problem ?
I would suggest to break down the wait until class into two lines for simplicity. Its totally optional and wouldn't make much of a difference.
wait = WebDriverWait(driver, 300)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".header__button--search")))
element_to_be_clicked=driver.find_element_by_css_selector(".header__button--search")
element_to_be_clicked.click()
For the second part try using the values of the options in drop down list:
fill_search_bar.clear()
fill_search_bar.send_keys(Team)
driver.find_element_by_xpath("//div[#class='dropDown__selectedValue dropDownValueSelected___3msxRQS']").click()
select_sport=Select(driver.find_element_by_class("dropDown__list dropDownList___3V-ppVu"))
select_sport.select_by_value('1') #football has value 1 in the list
Related
I was wondering if it's possible to look for children of an element with the presence_of_element_located function. I know I could just use the entire path, but that would make my code more confusing due to it's nature. My code would look something like this (much more complicated but this is the important bit):
currentEl = driver.find_element(By.XPATH, ("//*[#id='2']"))
func(currentEl)
def func(currentEl):
#Wait for the element to appear
WebDriverWait(driver, 10, poll_frequency=0.001, ignored_exceptions = (StaleElementReferenceException, NoSuchElementException)).until(
EC.presence_of_element_located((currentEl, (By.CLASS_NAME, "class")))
)
return
So basically I'm trying to wait for the child element with the class name "class" of the WebElement named currentEl. I'we tried many things and I'd hate to resort to just using the entire path.
Child element with class name className can be located by relative XPath .//*[contains(#class,'className')] or with relative CSS Selector .className.
So, I think your code can be modified to be
currentEl = driver.find_element(By.XPATH, ("//*[#id='2']"))
func(currentEl)
def func(currentEl):
#Wait for the element to appear
WebDriverWait(driver, 10, poll_frequency=0.001, ignored_exceptions = (StaleElementReferenceException, NoSuchElementException)).until(
EC.presence_of_element_located((currentEl, By.XPATH, ".//*[contains(#class,'className')]")))
return
Or
currentEl = driver.find_element(By.XPATH, ("//*[#id='2']"))
func(currentEl)
def func(currentEl):
#Wait for the element to appear
WebDriverWait(driver, 10, poll_frequency=0.001, ignored_exceptions = (StaleElementReferenceException, NoSuchElementException)).until(
EC.presence_of_element_located((currentEl, By.CSS_SELECTOR, ".className")))
return
UPD
I'm not sure the structure above will work.
What you can do is to pass correct locator to existing expected_conditions, as following:
locator1 = (By.XPATH, "//*[#id='2']//*[contains(#class,'className')]")
locator2 = (By.CSS_SELECTOR, "#2 .className")
def wait_for_child_element(locator):
WebDriverWait(driver, 30).wait.until(EC.presence_of_element_located(locator))
#call the method above with arguments:
wait_for_child_element(locator1)
wait_for_child_element(locator2)
I'm having trouble accessing a specific span with selenium. I want to click on a specific search result if the spans texts match a specific string from the website https://www.superherodb.com/battle/create/#add_member
The html code:
<span>
<img src="/pictures2/portraits/10/025/791.jpg" class="avatar avatar-sm" alt="Superman"> Superman
<span class="suffix level-1">Kal-El</span>
<span class="suffix level-2">Prime Earth</span>
</span>
I want to check if the string from the first span = Superman, and second span = Kal-El, and third span= Prime Earth, match the strings on a list.
My code:
driver = webdriver.Chrome('C:\Webdriver\chromedriver.exe', options= options)
driver.get('https://www.superherodb.com/battle/create')
wait = WebDriverWait(driver, 5)
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="team1"]/div/a'))).click()
Superheroes = ["Superman", "Spiderman"] #check in span
Names = ["Kal-El", "Peter Parker"] #check in span class = "suffix level-1"
Universes = ["Prime Earth", "Prime, Earth"] #check in span class = "suffix level-1"
wait.until(EC.visibility_of_element_located((By.NAME, 'quickselect'))).send_keys(Superheroes[0]) #Search for Superman
search = [my_elem for my_elem in WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH,
".//*[#id='quickselect_result']/li/span/[contains(text(), Superheroes[0] )]/..")))
if
my elem in WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH,
".//*[#id='quickselect_result']/li/span/span[1][#class='suffix level-1' and contains(text(), Names[0] )]/..")))
if
my_elem in WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH,
".//*[#id='quickselect_result']/li/span/span[2][#class='suffix level-2' and contains(text(), Universes [0])]/..")))
]
search.click()
My Error:
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression .//*[#id='quickselect_result']/li/span/[contains(text(), Superheroes[0] )]/.. because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string './/*[#id='quickselect_result']/li/span/[contains(text(), Superheroes[0] )]/..' is not a valid XPath expression.
I want to ultimately turn this into a for loop to check a huge list of Superheroes but I want to select the right one from the list because there are many versions of the same Superhero (i.e. same Superhero, but different Universe).
What am I doing that is wrong?
I am trying to scrape information from an automobile blog but i can't loop through the div tag containing the paragraph tags that contain the info.
driver.get("https://www.autocar.co.uk/car-news")
driver.maximize_window()
for i in range(3):
i+=1
info = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, f'//*[#id="page"]/div[2]/div[1]/div[1]/div[2]/div/div[1]/div/div[1]/div[1]/div[{i}]/div')))
heading = info.find_element_by_tag_name('h2')
clickable = heading.find_element_by_tag_name('a')
driver.execute_script("arguments[0].click();", clickable)
# the code starts to fail around here
try:
body_info = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'field-item even')))
main_text = []
for j in range(3):
j+=1
text = body_info.find_element_by_tag_name('p')
main_text.append(text)
for t in main_text:
t_info = t.text
print(f'{heading.text}\n{t_info}')
except:
print("couldn't find tag")
driver.back()
There's an issue with your code, (By.CLASS_NAME, 'field-item even').
Selenium does not have support for multiple classes or classes with space.
Simply replace space with . and that would be the CSS_SELECTOR
Try something like this:
try:
body_info = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.field-item.even')))
It must be '.field-item even' and not 'field-item even' if you are using By.CSS_SELECTOR for presence_of_element_located().
So Replace,
body_info = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'field-item even')))
with,
body_info = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'field-item even')))
Official docs. https://selenium-python.readthedocs.io/api.html#locate-elements-by
To select multiple classes, you must use the class selector. You can't select multiple classes through a space, like in CSS. You need to select multiple classes using the class selector. So you must put a dot before all of the classes and not give any space between them
I want to click the next page until no more page, but it does not click.
returns the error:raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: stale element reference: element is not attached to the page document
my codes:
Thanks in advance!
driver.get('http://www.chinamoney.com.cn/chinese/zjfxzx/?tbnm=%E6%9C%80%E6%96%B0&tc=null&isNewTab=1')
driver.implicitly_wait(10)
driver.refresh()
driver.implicitly_wait(10)
wait = WebDriverWait(driver, 5)
datefield_st = wait.until(EC.element_to_be_clickable((By.ID, "pdbp-date-1")))
datefield_st.click()
select_st = Select(wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "ui-datepicker-year"))))
select_st.select_by_visible_text("2021")
select2 = Select(wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "ui-datepicker-month"))))
select2.select_by_value("1")
day=1
wait.until(EC.element_to_be_clickable((By.XPATH, "//td[#data-handler='selectDay']/a[text()='{}']".format(str(day))))).click()
datefield_ed = wait.until(EC.element_to_be_clickable((By.ID, "pdbp-date-2")))
datefield_ed.click()
select_ed = Select(wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "ui-datepicker-year"))))
select_ed.select_by_visible_text("2021")
select2 = Select(wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "ui-datepicker-month"))))
select2.select_by_value("1")
day=1
wait.until(EC.element_to_be_clickable((By.XPATH, "//td[#data-handler='selectDay']/a[text()='{}']".format(str(day))))).click()
driver.find_element_by_link_text("查询").click()
while True:
driver.implicitly_wait(10)
links=[link.get_attribute('href') for link in driver.find_elements_by_xpath("//a[contains(#title,'同业存单') and not(contains(#title,'申购说明')) and not(contains(#title,'公告'))]")]
titles = [title.text for title in driver.find_elements_by_xpath("//a[contains(#title,'中期票据') and not(contains(#title,'申购说明')) and not(contains(#title,'公告'))]")]
dates = [date.text for date in driver.find_elements_by_xpath('//*[#class="san-grid-r text-date"]')]
driver.implicitly_wait(10)
for link, title,date in zip(links, titles,dates):
dataframe = pd.DataFrame({'col1':date,'col2':title,'col3':link},index=[0])
dataframe.to_csv('Chinamoney.csv',mode='a+',header=False,index=False,encoding='utf-8-sig')
print(link,title,date)
try:
driver.find_element_by_xpath('//*[contains(#class, "page-next")]').click()
except:
print('No more pages')
You passed two class names into selector while it's not allowed for search by class name. Either try
(By.CLASS_NAME, 'page-next')
or
(By.CSS_SELECTOR, '.page-btn.page-next')
Also your element and icon select the same element. So you don't need to define icon. Simply use element.click()
You are using:
driver.find_element_by_xpath('//*[contains(#class, "page-next")]').click()
Try:
element = driver.find_element_by_xpath('//*[contains(#class, "page-next")]')
driver.execute_script("arguments[0].click();", element)
If this doesnt work, you can try to obtain the url/link value and store it, and later you can go to the url or do what you want without click in it.
I've been using selenium ChromeDriver to parse some information. I've found elements I need by XPath, saved them into the list and then wrote a for loop to click on each of the element. It worked perfectly when I had a small amount of elements (>30), but now I'm facing a problem, that the length of my list is shorter than the actual amount of elements on the webpage (like 30 from 112, or 45 from 210). But all of these elements have the same XPath I've located initially.
# login function
def login(email,password):
driver.get('https://crm.tender-win.ru/account/logon')
driver.find_element_by_id('email').send_keys(email)
driver.find_element_by_id('password').send_keys(password)
driver.find_element_by_id('btnLogin').click()
df = []
login('somelogin','somepassword')
driver.get('https://crm.tender-win.ru/tenders/new')
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'MyTenderMenuTabsInfo')))
tenders = [driver.find_elements_by_xpath("//div[#class='panel card ']")
or driver.find_elements_by_xpath("//div[#class='panel card new']")][0]
for tender in tenders:
tender.click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'tenderCurrencyCode')))
data = driver \
.find_element_by_xpath('//*[#id="infoScroll"]/div/div[3]/div[2]/table') \
.get_attribute('outerHTML')
df1 = pd.read_html(data)
df.append(df1)
print(df)
P.S.
I solved my issuse by turning for loop into function and using it until my list with elemenets is empty.
def clicker(tenders=None):
for tender in tenders:
tender.click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'tenderCurrencyCode')))
data = driver \
.find_element_by_xpath('//*[#id="infoScroll"]/div/div[3]/div[2]/table') \
.get_attribute('outerHTML')
df1 = pd.read_html(data)
df.append(df1)
print(df)
def scrape():
tenders = [driver.find_elements_by_xpath("//div[#class='panel card ']")
or driver.find_elements_by_xpath("//div[#class='panel card new']")][0]
while len(tenders) > 0:
clicker(tenders)
tenders = [driver.find_elements_by_xpath("//div[#class='panel card ']")
or driver.find_elements_by_xpath("//div[#class='panel card new']")][0]