如何点击父类如果子类的href与要求匹配

huangapple go评论83阅读模式
英文:

How click the parent class if the child href matches the requirement

问题

I've tired to find the right selenium code to get click the main parent class if the following requirements exist in the class :

Parent Class

<div class ="col-xs-2-4 shopee-search-item-result__item" data-sqe="item">

Child class

<a data-sqe="link" href= all urls that is printed in python.>

Child class contains this element
<div class = "_1gkBDw _2O43P5">
<div class = "_1HvBLA">
<div class = "_3ao649" data-sqe="ad"> Ad</div>

Here is the code bellow

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import csv

import time
url = 'https://shopee.com.my/search?keyword=mattress'

driver = webdriver.Chrome(executable_path=r'E:/users/Francabicon/Desktop/Bots/others/chromedriver.exe')
driver.get(url)
time.sleep(0.8)

# select language
driver.find_element_by_xpath('//div[@class="language-selection__list"]/button').click()
time.sleep(3)

# scroll few times to load all items 
for x in range(10):
    driver.execute_script("window.scrollBy(0,300)")
    time.sleep(0.1)

# get all links (without clicking)

all_items = driver.find_elements_by_xpath('//a[@data-sqe="link"]')
print('len:', len(all_items))

all_urls = []

j = 0
k = 45

for item in all_items:
    url = item.get_attribute('href')
    all_urls.append(url)

print(all_urls)

a= len(all_urls)



# now use links
i = 0


while i <= 4 :
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='col-xs-2-4 shopee-search-item-result__item' and @data-sqe='item']//a[@class='link' and @href= all_urls[i]]"))).click()
    i+=1

I've tried to locate:
-Div the whole class
-locate classes and the href individually
-click the first five columns
but it all always fails.

Traceback (most recent call last):
  File "E:/Users/Asashin/Desktop/Bots/click test 7.py", line 52, in <module>
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='col-xs-2-4 shopee-search-item-result__item' and @data-sqe='item']//a[@class='link' and @href= all_urls[i]]"))).click()
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Can I be solved?

英文:

Hi I've tired to find the right selenium code to get click the main parent class if the following requirements exist in the class :

Parent Class

&lt;div class =&quot;col-xs-2-4 shopee-search-item-result__item&quot; data-sqe=&quot;item&quot;&gt;

Child class

&lt;a data-sqe=&quot;link&quot; href= all urls that is printed in python.&gt;

Child class contains this element
&lt;div class = &quot;_1gkBDw _2O43P5&quot;&gt;
&lt;div class = &quot;_1HvBLA&quot;&gt;
&lt;div class = &quot;_3ao649&quot; data-sqe=&quot;ad&quot;&gt; Ad&lt;/div&gt;


如何点击父类如果子类的href与要求匹配

Here is the code bellow

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import csv

import time
url = &#39;https://shopee.com.my/search?keyword=mattress&#39;

driver = webdriver.Chrome(executable_path=r&#39;E:/users/Francabicon/Desktop/Bots/others/chromedriver.exe&#39;)
driver.get(url)
time.sleep(0.8)

# select language
driver.find_element_by_xpath(&#39;//div[@class=&quot;language-selection__list&quot;]/button&#39;).click()
time.sleep(3)

# scroll few times to load all items 
for x in range(10):
    driver.execute_script(&quot;window.scrollBy(0,300)&quot;)
    time.sleep(0.1)

# get all links (without clicking)

all_items = driver.find_elements_by_xpath(&#39;//a[@data-sqe=&quot;link&quot;]&#39;)
print(&#39;len:&#39;, len(all_items))

all_urls = []

j = 0
k = 45

for item in all_items:
    url = item.get_attribute(&#39;href&#39;)
    all_urls.append(url)

print(all_urls)

a= len(all_urls)



# now use links
i = 0


while i &lt;= 4 :
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//div[@class=&#39;col-xs-2-4 shopee-search-item-result__item&#39; and @data-sqe=&#39;item&#39;]//a[@class=&#39;link&#39; and @href= all_urls[i]]&quot;))).click()
    i+=1

I've tried to locate:
-Div the whole class
-locate classes and the href individualy
-click the first five columns
but it all always fails.

Traceback (most recent call last):
  File &quot;E:/Users/Asashin/Desktop/Bots/click test 7.py&quot;, line 52, in &lt;module&gt;
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//div[@class=&#39;col-xs-2-4 shopee-search-item-result__item&#39; and @data-sqe=&#39;item&#39;]//a[@class=&#39;link&#39; and @href= all_urls[i]]&quot;))).click()
  File &quot;C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py&quot;, line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

Can I be solved?

答案1

得分: 1

我已经做了一些更改。

  1. 当您获取href值时,您获取的是完整的URL,而不是在DOM中看到的URL,所以您需要删除前面的部分以便以后验证。
  2. 在最后的while循环中,all_urls[i]是一个变量,您需要将它传递为变量而不是字符串。
  3. 一旦您点击每个链接,您需要使用driver.back()返回到父页面。
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
import time

url = 'https://shopee.com.my/search?keyword=mattress'

driver = webdriver.Chrome(executable_path=r'E:/users/Francabicon/Desktop/Bots/others/chromedriver.exe')
driver.get(url)

# 选择语言
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//div[@class="language-selection__list"]/button'))).click()
time.sleep(3)

# 滚动几次以加载所有项目
for x in range(10):
    driver.execute_script("window.scrollBy(0,300)")
    time.sleep(0.1)

# 获取所有链接(不点击)
all_items = driver.find_elements_by_xpath('//a[@data-sqe="link"]')
print('len:', len(all_items))

all_urls = []

j = 0
k = 45

for item in all_items:
    # 这将给您锚标签的完整URL
    url = item.get_attribute('href')
    # 为了以后点击验证href,您需要删除前面的部分
    urlfinal = url.split('https://shopee.com.my')[1]
    all_urls.append(urlfinal)

print(all_urls)
a = len(all_urls)

# 现在使用链接
i = 0

while i <= 4:
    # 通过子标签识别父标签,使用以下XPath。
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='col-xs-2-4 shopee-search-item-result__item' and @data-sqe='item'][.//a[@data-sqe='link' and @href='" + all_urls[i] + "']]"))).click()
    driver.back()
    i += 1
英文:

I have made couple of changes.

>1. When you are fetching href values you are getting complete url and not the url you are seeing in DOM so you need to remove the preceding values in order to verify later.
>
>2. In the last while loop all_urls[i] is variable you need passed it as variable not string.
>
>3. Once you click each link you need to come back to the parent page again by using driver.back()

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
import time
url = &#39;https://shopee.com.my/search?keyword=mattress&#39;

driver = webdriver.Chrome(executable_path=r&#39;E:/users/Francabicon/Desktop/Bots/others/chromedriver.exe&#39;)
driver.get(url)
# select language
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,&#39;//div[@class=&quot;language-selection__list&quot;]/button&#39;))).click()
time.sleep(3)

# scroll few times to load all items
for x in range(10):
    driver.execute_script(&quot;window.scrollBy(0,300)&quot;)
    time.sleep(0.1)

# get all links (without clicking)
all_items = driver.find_elements_by_xpath(&#39;//a[@data-sqe=&quot;link&quot;]&#39;)
print(&#39;len:&#39;, len(all_items))

all_urls = []

j = 0
k = 45

for item in all_items:
    # This give you whole url of the anchor tag
    url = item.get_attribute(&#39;href&#39;)
    # You need to remove the preceding values in order to verify href later for clicking
    urlfinal=url.split(&#39;https://shopee.com.my&#39;)[1]
    all_urls.append(urlfinal)

print(all_urls)
a= len(all_urls)

# now use links
i = 0

while i &lt;= 4 :
    #Identify the parent tag by child tag use following Xpath.
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//div[@class=&#39;col-xs-2-4 shopee-search-item-result__item&#39; and @data-sqe=&#39;item&#39;][.//a[@data-sqe=&#39;link&#39; and @href=&#39;&quot; + all_urls[i] +&quot;&#39;]]&quot;))).click()
    driver.back()
    i+=1

答案2

得分: 0

如何获取父元素:

child_element = driver.find_element_by_xpath('//a[@data-sqe="link"]')
parent_element = child_element.find_element_by_xpath('./ancestor::divcontains(@class, "shopee-search-item-result__item")')

如何获取具有特定子元素的元素:

element = driver.find_element_by_xpath('div[contains(@class, "shopee-search-item-result__item") and .//a[@data-sqe="link"]]')

英文:

How to get parent element:

child_element = driver.find_element_by_xpath(&#39;//a[@data-sqe=&quot;link&quot;]&#39;)
parent_element = child_element.find_element_by_xpath(&#39;./ancestor::div[contains(@class, &quot;shopee-search-item-result__item&quot;)][1]&#39;)

How to get element with specific child:

element = driver.find_element_by_xpath(&#39;div[contains(@class, &quot;shopee-search-item-result__item&quot;) and .//a[@data-sqe=&quot;link&quot;]]&#39;)

huangapple
  • 本文由 发表于 2020年1月3日 14:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定