how to click on the first result of a search on metacritc with selenium - python-3.x

how do I click on the first search result on metacritc's search bar?
this is what I have so far:
self.search_element = self.driver.find_element_by_name("search_term")
self.search_element.clear()
self.search_element.send_keys(self.game_line_edit.text())
self.link_to_click = self.driver.find_element_by_name("search_results_item")
self.link_to_click.click()
# self.game.setText(self.driver.find_element("search_results_item"))
self.game_line_edit.setText("")
self.driver.close()
but I'm getting this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"search_results_item"}
I realize selenium can't find the link but I am not sure what element it is
this is the HTML I'm trying to click on:
<a class="search_results_item" href="https://www.metacritic.com/game/pc/into-the-breach">
<span class="metascore_w score_outstanding">90</span>
<span class="title" data-mctitle="Into the Breach"><b>Into the</b> Breach</span>
<span class="type secondary">PC Game</span>
<span class="separ secondary">,</span>
<span class="date secondary">2018</span>
</a>
can someone help?
Thanks!

You are searching by name when you are referring to a class. Instead use a CSS selector, e.g.
.find_element_by_css_selector(".search_results_item")
.search_results_item indicates a class with the name 'search_results_item'.
If that doesn't work, you probably need a wait. See this answer for more info.

If you're fine with using xPath, you can select by index.
(//a[contains(#class,'search_results_item')])[1]
#JeffC is also correct as well. You should not select by name because this element has no name. At the very least, select by class or tag.

Related

Selenium Can't Find Element Returning None or []

im having trouble accessing element, here is my code:
driver.get(url)
desc = driver.find_elements_by_xpath('//p[#class="somethingcss xxx"]')
and im trying to use another method like this
desc = driver.find_elements_by_class_name('somethingcss xxx')
the element i try to find like this
<div data-testid="descContainer">
<div class="abc1123">
<h2 class="xxx">The Description<span data-tid="prodTitle">The Description</span></h2>
<p data-id="paragraphxx" class="somethingcss xxx">sometext here
<br>text
<br>
<br>text
<br> and several text with
<br> tag below
</p>
</div>
<!--and another div tag below-->
i want to extract tag p inside div class="abc1123", but it doesn't return any result, only return [] when i try to get_attribute or extract it to text.
When i try extract another element using this method with another class, it works perfectly.
Does anyone know why I can't access these elements?
Try the following css selector to locate p tag.
print(driver.find_element_by_css_selector("p[data-id^='paragraph'][class^='somethingcss']").text)
OR Use get_attribute("textContent")
print(driver.find_element_by_css_selector("p[data-id^='paragraph'][class^='somethingcss']").get_attribute("textContent"))

How to click on Web check box using Excel VBA?

How do I check the table checkbox?
I tried clicking.
ie.Document.getElementsByClassName("x-grid3-hd-checker").Checked = True
<div class="x-grid3-hd-inner x-grid3-hd-checker x-grid3-hd-checker-on" unselectable="on" style="">
<a class="x-grid3-hd-btn" href="#"></a>
<div class="x-grid3-hd-checker"> </div>
<img class="x-grid3-sort-icon" src="/javascript/extjs/resources/images/default/s.gif">
</div>
I can't see a checkbox in the HTML code. But you use getElementsByClassName() in a wrong way for your case. getElementsByClassName() generates a node collection. If you need a specific node, you must get it by it's index in the node collection. First element has index 0.
Please note that the div tag with the CSS class class="x-grid3-hd-inner x-grid3-hd-checker x-grid3-hd-checker-on " is also included in the Node Collection, because a part of the class identifier is identical to "x-grid3-hd-checker ". [Edit: I'm not realy sure if the part must maybe stand at the begin of the identifier]
If you want to check this:
<div class="x-grid3-hd-checker"> </div>
Your code needs the second index of the node collection:
ie.Document.getElementsByClassName("x-grid3-hd-checker")(1).Checked = True
But if there are more tags with the class name "x-grid3-hd-checker" the above line don't work. I can't say anymore until you don't post more HTML and VBA code. The best would be a link to the site.

How can i click the third href link?

<ul id='pairSublinksLevel1' class='arial_14 bold newBigTabs'>...<ul>
<ul id='pairSublinksLevel2' class='arial_12 newBigTabs'>
<li>...</li>
<li>...</li>
<li>
<a href='/equities/...'> last data </a> #<-- HERE
</li>
<li>...</li>
Question is how can i get click third li tag ??
In my code
xpath = "//ul[#id='pairSublinksLevel2']"
element = driver.find_element_by_xpath(xpath)
actions = element.find_element_by_css_selector('a').click()
code works partially. but i want to click third li tag.
The code keeps clicking on the second tag.
Try
driver.find_element_by_xpath("//ul[#id='pairSublinksLevel2']/li[3]/a").click()
EDIT:
Thanks #DebanjanB for suggestion:
When you get the element with xpath //ul[#id='pairSublinksLevel2'] and search for a tag in its child elements, then it will return the first match(In your case, it could be inside second li tag). So you can use indexing as given above to get the specific numbered match. Please note that such indexing starts from 1 not 0.
As per the HTML you have shared you can use either of the following solutions:
Using link_text:
driver.find_element_by_link_text("last data").click()
Using partial_link_text:
driver.find_element_by_partial_link_text("last data").click()
Using css_selector:
driver.find_element_by_css_selector("ul.newBigTabs#pairSublinksLevel2 a[href*='equities']").click()
Using xpath:
driver.find_element_by_xpath("//ul[#class='arial_12 newBigTabs' and #id='pairSublinksLevel2']//a[contains(#href,'equities') and contains(.,'last data')]").click()
Reference: Official locator strategies for the webdriver

Python Selenium can't select dropdown (chrome webdriver)

I have a dropdown element, I want to select the All option, the corresponding HTML code is:
<div class="dataTables_length" id="indicators_length">
<label>
<span class="result-mune">
<span>Results </span>per page:
</span>
<select name="indicators_length" aria-controls="indicators" class="jcf-hidden">
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="-1">All</option>
</select>
<span class="jcf-select jcf-unselectable">
<span class="jcf-select-text">
<span class="">25</span>
</span>
<span class="jcf-select-opener"></span>
</span>
</label>
</div>
the select element is not highlighted using the browser Inspect method, looks like this drop down is triggered by js.
I tried to use the Select class described here:
select = Select(self._wait.until(EC.presence_of_element_located_by((By.XPATH, "//div[#id = 'indicators_length']//select[#name = 'indicators_length']")))
select.select_by_value('-1')
not working. and ActionChain method and even execute_script method, all not working. This bothers me a lot, hope somebody can help.
you don't really need to select the option just click the span and it will set the option automatically.
driver = webdriver.Chrome()
driver.get("......")
# click "OK, I Agree" cookies
driver.find_element_by_css_selector('.agree-button.eu-cookie-compliance-default-button').click()
# scroll down to make dropdown option visible
driver.find_element_by_css_selector('h4.pane-title').location_once_scrolled_into_view
select = driver.find_element_by_xpath('//span[#class="result-mune"]/following-sibling::span')
select.click()
# wait until javascript generate fake option element because it actually span
time.sleep(1)
select.find_element_by_css_selector('ul li span[data-index="4"]').click()
try this one:
driver.execute_script('document.getElementsByName("indicators_length")[0].value = 50;
If its created and loaded after page load make sure you add some sleep to let it render;
I tried using the Selenium Select class, it can find the element but it cannot select an option from the element. Not sure whats going on, could be the class "jcf-hidden" on the element.
Having said that, I took a stab at it and below is my approach. Try it out, it worked on my system, you have to handle the clicking of "OK, I Agree" button click, I did that manually.
import time
from selenium.webdriver import Chrome
driver = Chrome()
driver.get('https://www.topuniversities.com/university-rankings/world-university-rankings/2019')
# Remove this nasty time.sleep and implement webdriver wait
# Handle the OK, I Agree
time.sleep(5)
i_agree = driver.find_element_by_css_selector('button[class*="eu-cookie-compliance-default-button"]')
i_agree.click()
time.sleep(5)
# Open the Select
rankings_length = driver.find_element_by_id('qs-rankings_length')
select_opener = rankings_length.find_element_by_class_name('jcf-select-opener')
select_opener.click()
# loop through the list
select_content = rankings_length.find_element_by_class_name('jcf-list-content')
for content in select_content.find_elements_by_tag_name('li'):
if content.text == 'All':
content.click()

Select checkbox using Selenium with Python3.x and Selenium

I'm trying to check the checkbox on a page: https://www.pkobp.pl/poi/?clients=1,2,3.
<li class="poi-filter-top__el">
<div class="poi-icon poi-icon--facility"></div>
<input type="checkbox" id="poi-legend-facility" class="js-poi-legend" name="type" value="facility"> <label for="poi-legend-facility" class="poi-legend input-checkbox poi-filter-top__label">OddziaƂ</label> Wybierz rodzaj
</li>
I try to do this with:
checkboxes = driver.find_elements_by_id("poi-legend-facility")
for checkbox in checkboxes:
if not checkbox.is_selected():
checkbox.click()
But it doesn't work. Can you help me?
You have only one checkbox with such ID (poi-legend-facility).
You can do like this:
checkbox = driver.find_element_by_id("poi-legend-facility")
if not checkbox.is_selected():
checkbox.click()
Or in your case try this code:
checkboxes = driver.find_elements_by_id("poi-legend-facility")
checkboxes[0].click()
PS: Using ID for finding element faster than finding by XPath.
This is the the only way I could find to check all the boxes in that page and break out of loop. Give this a shot as well.
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www.pkobp.pl/poi/?clients=1,2,3')
for tickbox in driver.find_elements_by_css_selector(".input-checkbox"):
try:
tickbox.click()
time.sleep(7)
except:
break
driver.quit()
Try this xpath
checkElements= driver.find_element_by_xpath("//input[#type='checkbox' and #value='facility']")
checkElements.click()
I don't know python so might be syntax error but path is correct

Resources