0

I am trying to automate speedtests with different browsers automatically, and the main part of the test is inside a loop. The problem is, sometimes, one element which has been selected before, and the script worked correctly, at the one of the next steps, exactly at the same loop and at the same page, but with different number, without any change in the xpath, selenium cannot select it again. So, I can not repeat my test as much as I want.

Most of the time I have this problem with Edge, and I think one reason can be, the xpath for elements which I found by help of Chrome or Firefox. ( I can not find the xpath in Edge first of all, I searched a lot about it).

I also put the different xpath that I use. Actually I want to get the numeric or string values of ping,download, upload location and server.

Please let me know, how can I solve this issue, I tried different sleep time and two different xpath. the script always gives me error when I am trying to select the element with class_name or css_selector.

firefox: "/html/body/div[3]/div[2]/div/div/div/div[3]/div[1]/div[3]/div/div[3]/div/div[1]/div[2]/div[1]/div/div[2]/span"

chrome: "//[@id='container']/div[2]/div/div/div/div[3]/div[1]/div[3]/div/div[3]/div/div[1]/div[2]/div[1]/div/div[2]/span"

chrome:

"//div[@class='result-item result-item-ping updated']/div[2]/span"

Other question is how can I wait for a page to load completely. this method WebDriverWait(driver,some seconds) does not work for me and i have to use time.sleep()

Error: selenium.common.exceptions.NoSuchElementException: Message: No such element

element = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div/div/div/div[3]/div[1]/div[3]/div/div[3]/div/div[1]/div[2]/div[1]/div/div[2]/span")

TylerH
  • 20,799
  • 66
  • 75
  • 101
A D
  • 585
  • 1
  • 7
  • 16
  • can you mentioned which `span` text are you looking after? – KunduK Mar 26 '19 at 12:09
  • Sorry, but I did not understand your question ?! Actually I want to get the numeric or string values of ping,download, upload location and server. `2` – A D Mar 26 '19 at 12:37

5 Answers5

2

To automate the speedtests you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    
    driver = webdriver.Edge(executable_path=r'C:\WebDrivers\MicrosoftWebDriver.exe')
    driver.get("https://www.speedtest.net/")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.js-start-test.test-mode-multi"))).click()
    WebDriverWait(driver, 45).until(EC.url_contains("result"))
    print("Ping :"+driver.find_element_by_css_selector("div[title='Reaction Time'] div.result-data.u-align-left>span").get_attribute("innerHTML"))
    print("Download: "+driver.find_element_by_css_selector("div[title='Receiving Time'] div.result-data.u-align-left>span").get_attribute("innerHTML"))
    print("Upload :"+driver.find_element_by_css_selector("div[title='Sending Time'] div.result-data.u-align-left>span").get_attribute("innerHTML"))
    #driver.quit()
    
  • Console Output:

    Ping :35
    Download: 21.53
    Upload :3.46
    
  • Browser Snapshot:

Edge_again

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for your response, for sure I can get an idea. but the problem with your code for me is, first i have to remove a ![banner](https://ibb.co/nz6g7vK) from the page then I can continue. It also happens when I have to get the results from the result page. I don't know how did you solve this problem, but for the line 5-8 that you wrote did not work. @DebanjanB – A D Mar 26 '19 at 21:23
  • If you read this comment do you know why I see this elements in chrome and firefox , but I don't see it in Edge? is a better way to remove it except: `driver.find_element_by_xpath("//*[@id='_evidon-banner-cookiebutton']").click() driver.find_element_by_xpath("//*[@id='_evidon-l3']/button").click()` – A D Mar 26 '19 at 21:23
  • @SD _line 5-8_ were configuration for _ChromeDriver_ / _Chrome_ combo for optimized performance. Incase you are using _Edge Browser_ you can omit them. Rest of the code should remain same. The _Banner_ simply doesn't obstructs the readings, so you don't need to handle that either. However I have updated the answer using _Edge Browser_. Let me know the status. – undetected Selenium Mar 26 '19 at 22:08
  • 1
    The code worked for Edge correctly and faster. but for ![Chrome](https://ibb.co/X34rJkQ) and ![Firefox](https://ibb.co/9pkGN7f) still banner makes problem. I have added the screenshot. I mean I should handle them. – A D Mar 27 '19 at 08:30
  • The same code will work flawless on _Chrome_ as well if you keep the _Chrome Browser_ **maximized** as I suggested in the initial version of my answer where you had a cross question for **line 5-8** which I have explained in my comment as **line 5-8 were configuration for ChromeDriver / Chrome combo for optimized performance** – undetected Selenium Mar 27 '19 at 08:35
  • I have problem in choosing a specific server and location. There are more than one server for a location. I am trying to click on my desire element by mouse hover. I really appreciate for you help if you can give me a suggestion I put the that part of the code in another comment. the code is inserted there in a way that can handle character limitation for comment @DebanjanB. – A D Mar 27 '19 at 08:48
  • Ok I put another question, Thank you again – A D Mar 27 '19 at 08:53
  • Unfortunately even by adding that lines, still the code does not work for[Chrome](https://ibb.co/1JqBvvD) – A D Mar 27 '19 at 09:39
  • Hmmm, even I have the same configuration _ChromeDriver v2.46_ and _Chrome v73.x_. Can you tell me which is that element overlapping our desired element? Somehow, I don't find the element during my tests ... – undetected Selenium Mar 27 '19 at 09:47
  • On another note, if you are on a slower/faster network, can you increase the _timeout_ to 20 seconds? – undetected Selenium Mar 27 '19 at 09:49
  • This [banner](https://ibb.co/sq8Vx6h) element number one,makes problem in chrome, Firefox and safari, edge does not have this problem. For repetition of the test in Firefox I have to remove number 2 (privacy) in the page. Also, I increased the timeout but did not work. I am working on 100 Mbps, the goal is to test 1 Gbps. – A D Mar 27 '19 at 10:41
  • @SD As this question was majorly focused on retrieving the `Ping`, `Download` and `Upload` value, I would suggest you to raise a new question for the _banner_ issue. StackOverflow contributors will be happy to help you out. – undetected Selenium Mar 27 '19 at 10:46
1

Use the following CSS locators to identify the values:

Download: *.result-data-large.number.result-data-value.download-speed*

Upload: *.result-data-large.number.result-data-value.upload-speed*

Ping: *.result-data-large.number.result-data-value.ping-speed*

Making use of getText(), you can retrieve their values. Wait for an element in the page to be visible to make sure the page is loaded successfully.

jeanggi90
  • 741
  • 9
  • 24
  • Thank you for your answer, even i increased the sleep time, and i used your suggestion I get error again: `element = driver.find_element_by_css_selector("*.result-data-large.number.result-data-value.ping-speed*")` => No such element `element = driver.find_element_by_css_selector("*.result-data-large.number.result-data-value.ping-speed*").getText()` => No such element – A D Mar 26 '19 at 13:09
1

Try with:element = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div/div/div/div[3]/div[1]/div[3]/div/div[3]/div/div[1]/div[2]/div[1]/div/div[2]/")

Maybe also you need to catch exception for: NoSuchElementException cases.

1

I've tested these CSS selectors and they work in both Chrome and Edge.

span.ping-speed                         # ping
span.download-speed                     # download
span.upload-speed                       # upload
div.server-current > div.result-label   # server

If you want to know when the page is done loading, you can wait until the URL changes from https://www.speedtest.net to https://www.speedtest.net/results/<some number>. I would just use WebDriverWait and url_contains("results") , e.g.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
WebDriverWait(driver, 10).until(EC.url_contains("results"))

There are some other approaches in this question.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Yes, you are right, but i used `driver.get("https://www.speedtest.net/results")` and i used time.sleep(). I think the problem could be the fact that i used `WebDriverWait(driver, 10)` without `until(EC.url_contains("results"))`. But, still I don't understand why sometimes an element can be find and sometimes it gives me no such an element error !!! Thank you for your response. I hope it works tommorow in the Lab. – A D Mar 26 '19 at 21:01
  • You can just navigate to the base URL and click the GO button. The rest of the code will work fine. – JeffC Mar 26 '19 at 22:05
0
    WebDriverWait driverWait = new WebDriverWait(driver, 30000);
    driver.get("https://www.speedtest.net/");
    WebElement goLink = driver.findElement(By.cssSelector(".js-start-test.test-mode-multi"));
    driverWait.until(ExpectedConditions.elementToBeClickable(goLink));
    goLink.click();

    By download = By.cssSelector(".result-data-large.number.result-data-value.download-speed");
    By upload = By.cssSelector(".result-data-large.number.result-data-value.upload-speed");
    By ping = By.cssSelector(".result-data-large.number.result-data-value.ping-speed");

    driverWait.until(ExpectedConditions.urlMatches("https://www.speedtest.net/result/[0-9]"));
    String downloadSpeed = driver.findElement(download).getText();
    String uploadSpeed = driver.findElement(upload).getText();
    String pingValue = driver.findElement(ping).getText();
    System.out.println("Download: "+downloadSpeed + "\nUpload: "+ uploadSpeed + "\n Ping: "+pingValue);

Output Download: 78.82 Upload: 45.93 Ping: 23

enter image description here